【问题标题】:separate the size of array and array value by delimiter -1 in c++在c ++中通过分隔符-1分隔数组和数组值的大小
【发布时间】:2020-10-18 06:07:51
【问题描述】:

用分隔符-1分隔数组大小和数组值

输入格式为:

Array-size -1 array-values(separated by ,) -1 number -1
3 -1 3,5,7 -1 6 -1

不要使用向量,将值放入用户给定大小的数组中,否则打印“-1”。

我已经尝试过这段代码,但我无法完成它:

#include <iostream>
#include <string>
    
using namespace std;
    
int main(){
    string s{};
    getline(cin, s)
    stringstream ss(s);

    string count = size_t{};
    string delimiter1 = string{};
    string value = string{};
    string delimiter2 = string{};
    string size = string{};
    string delimiter3 = string{};

    if(!(ss >> count >> delimiter1 >> values >> delimiter2 >> size >> delimiter2))
        cout << -1 << endl;

    if(delimiter1 != "-1" || delimiter2 != "-1" || delimiter3 != "-1")
        cout << -1 << endl;

    int a[1000];

    while(getline(ss, value, ',')){
        a.push_back(stoi(value));
    }
}

【问题讨论】:

  • 为什么“不使用矢量”?将值存储在 std::vector 中似乎是完美的选择。
  • 您的实际问题是什么?你知道普通的 C 风格数组没有 push_back 方法,不是吗?
  • @Jessica 这不是一个问题,只是陈述一个要求。你的问题是什么?这是一个问答网站。我们在这里回答问题。
  • @Jessica 你能告诉我们更多吗 - 存储数组后你应该做什么(预期输出)?最后那个数字的作用是什么(在你的例子中是6)?
  • 好的,你可以使用std::unique_ptr吗?

标签: c++ delimiter


【解决方案1】:

您的代码似乎在一百万英里之外,但您没有标记values,并且您在错误的位置初始化了ss,并在您的意思是delimiter3的地方使用了delimiter2,而您尝试在数组上使用push_back

其他错误是将整数值作为字符串读取,这似乎为您自己做更多的工作。

最后,既然您知道要读取多少个整数,您可能应该使用该信息。

这段代码看起来更接近

int main() {
    // read line of data
    string s;
    getline(cin, s)

    // initial parse of data
    size_t count, size;
    int delimiter1, delimiter2, delimiter3;
    string values;
    if (!(ss >> count >> delimiter1 >> values >> delimiter2 >> size >> delimiter3))
        std::cerr << "error here\n";

    // check delimiters
    if (delimiter1 != -1 || delimiter2 != -1 || delimiter3 != -1)
        std::cerr << "error here\n";

    // read count integers into array
    int a[1000];
    stringstream ss(values);
    string value;
    for (size_t i = 0; i < count; ++i)
         if (!getline(ss, value, ',')) {
             std::cerr << "error here\n";
         }
         a[i] = stoi(value);
    }
}

【讨论】:

  • 你能解释更多我被困在这里并且无法编写更多代码,如何按照“数组大小 -1 数组值(由 , 分隔)-1 数字 - 的形式指定输入1" 并将值存储到数组 'a' 中。
  • @Jessica 我已经更新了代码,还有什么不清楚的地方?
  • 请解释幻数“1000”。是否有理由假设永远不需要更大的数组?除了“OP做出同样的毫无根据的假设”。我是说。
【解决方案2】:

我认为这么多代码就可以了。如果输入格式错误或n 非常大,它将在stderr 上打印-1

#include <iostream>
#include <istream>
#include <string>

int main() {
  try {
    int n, _, number;
    std::string str;
    std::cin.exceptions(std::istream::failbit);
    std::cin >> n >> _;
    int *arr = new int[n];
    for (int i = 0, temp; i < n; ++i) {
      std::cin >> temp;
      arr[i] = temp;
      std::cin >> std::ws;
      std::cin.ignore();
    }
    std::cin >> _ >> number >> _;
    // do something with array and number
    for (int i = 0; i < n; ++i)
      std::cout << arr[i] << ' ';
    std::cout << '\n' << number;
  } catch (...) {
    std::cerr << -1;
  }
}

示例运行:https://wandbox.org/permlink/hfcXf4BTXQSwCiVm

【讨论】:

    猜你喜欢
    • 2012-03-15
    • 2011-02-03
    • 1970-01-01
    • 2017-01-29
    • 1970-01-01
    • 2011-04-21
    • 1970-01-01
    • 2020-04-21
    • 2011-11-12
    相关资源
    最近更新 更多