Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从STDIN中读取数据,对于C++中给定的测试用例_C++_Stdin - Fatal编程技术网

如何从STDIN中读取数据,对于C++中给定的测试用例

如何从STDIN中读取数据,对于C++中给定的测试用例,c++,stdin,C++,Stdin,这听起来可能很傻,但这是我第一次在线解决编程竞赛。问题通常描述为: Input: First line indicates the number of test cases t.For the next 't' lines the data is entered. 我已经用正确的标题编写了以下程序: vector<string> read_strings(int t_cases) { vector<string> ip_vec; string line,

这听起来可能很傻,但这是我第一次在线解决编程竞赛。问题通常描述为:

Input:
First line indicates the number of test cases t.For the next 't' lines the data is entered.
我已经用正确的标题编写了以下程序:

vector<string> read_strings(int t_cases) {
    vector<string> ip_vec;
    string line, str;
    int cnt = 0;

    while (cnt != t_cases-1) {
        std::getline(std::cin, line);
        ++cnt;
    }

    std::istringstream iss(line);
    while (iss >> str) {
        ip_vec.push_back(str);
    }
    return ip_vec;
}
但是这个程序总是卡在一个输入循环中。我还尝试通过将iss放在第一个while循环中,在输入行后立即解析该行。如果有人能给我一个如何解决这个问题的指针,我将能够最终测试程序的其余部分


谢谢。

您需要在读取这些行时将它们添加到向量中。第一个while循环正在读取所有测试用例,但没有将它们保存在某个地方。然后下一个while循环尝试读取行,这是最后一次读取测试用例。请尝试以下操作:

vector<string> ip_vec;
string line, str;
for (int cnt = 0; cnt < t_cases; cnt++) {
   std::getline(std::cin, line);
   ip_vec.push_back(line);
}

您需要在读取直线时将其添加到向量中。第一个while循环正在读取所有测试用例,但没有将它们保存在某个地方。然后下一个while循环尝试读取行,这是最后一次读取测试用例。请尝试以下操作:

vector<string> ip_vec;
string line, str;
for (int cnt = 0; cnt < t_cases; cnt++) {
   std::getline(std::cin, line);
   ip_vec.push_back(line);
}

既然您开始学习编程竞赛是如何工作的,我不建议您存储所有给定的输入。通常,您可以存储输入的数量t,对于每个测试用例,程序在读取下一个测试用例之前输出该测试的答案。例如:

for (int i = 1; i <= t; i++) {
    cin >> input;
    // Some computations specific to the problem
    cout << output << endl; // Pay attention on how the problem wants the output to be printed.
}
while (cin >> a >> b >> c) {
    // Computations...
    cout << output << endl;
}
如果问题没有给出测试用例的数量,您可以简单地将循环更改为:

while (cin >> input) {
    // Computations...
    cout << output << endl;
}
这通常是我解决编程竞赛问题的方式

编辑:正如@AnkitKulshrestha所指出的,如果您必须为每个测试用例读取多个输入,那么您可以通过三个输入简单地读取它们,例如:

for (int i = 1; i <= t; i++) {
    cin >> input;
    // Some computations specific to the problem
    cout << output << endl; // Pay attention on how the problem wants the output to be printed.
}
while (cin >> a >> b >> c) {
    // Computations...
    cout << output << endl;
}

既然您开始学习编程竞赛是如何工作的,我不建议您存储所有给定的输入。通常,您可以存储输入的数量t,对于每个测试用例,程序在读取下一个测试用例之前输出该测试的答案。例如:

for (int i = 1; i <= t; i++) {
    cin >> input;
    // Some computations specific to the problem
    cout << output << endl; // Pay attention on how the problem wants the output to be printed.
}
while (cin >> a >> b >> c) {
    // Computations...
    cout << output << endl;
}
如果问题没有给出测试用例的数量,您可以简单地将循环更改为:

while (cin >> input) {
    // Computations...
    cout << output << endl;
}
这通常是我解决编程竞赛问题的方式

编辑:正如@AnkitKulshrestha所指出的,如果您必须为每个测试用例读取多个输入,那么您可以通过三个输入简单地读取它们,例如:

for (int i = 1; i <= t; i++) {
    cin >> input;
    // Some computations specific to the problem
    cout << output << endl; // Pay attention on how the problem wants the output to be printed.
}
while (cin >> a >> b >> c) {
    // Computations...
    cout << output << endl;
}

我喜欢这个建议,但是如果你在做需要检测换行符或任何空格的操作,你需要在for循环中运行另一个循环。另外,cin在空格后停止读取。因此,如果您的值为10 20 30 40,那么在某些指令导致istream刷新之前,cin将保持10。@AnkitKulshrestha,作为第一条注释,虽然这是一个更具体的问题,但我认为它不会导致重大的性能问题,至少我不记得必须处理这个问题,我编辑了这篇文章,针对您有多个输入的情况。我今晚会尝试一下,然后在这里与您联系。谢谢你的努力,虽然我喜欢这个建议,但是如果你在做需要检测换行符或任何空格的操作,你需要在for循环中运行另一个循环。另外,cin在空格后停止读取。因此,如果您的值为10 20 30 40,那么在某些指令导致istream刷新之前,cin将保持10。@AnkitKulshrestha,作为第一条注释,虽然这是一个更具体的问题,但我认为它不会导致重大的性能问题,至少我不记得必须处理这个问题,我编辑了这篇文章,针对您有多个输入的情况。我今晚会尝试一下,然后在这里与您联系。感谢您的努力哦,那么,一旦它读取了i/P,它就无法知道测试用例已经发生了吗?哦,那么,一旦它读取了i/P,它就无法知道测试用例已经发生了吗?