C++ 未知输入大小

C++ 未知输入大小,c++,input,cin,C++,Input,Cin,我相信这是一个简单的问题。老实说,这应该是编写SAT解算器最简单的部分,但是,我应该让用户输入如下数据: Sample Input: 1 <-- 1st number denotes the number of cases I will have 5 <-- 2nd number represents the number of variables, followed 1 2 3 who knows ho

我相信这是一个简单的问题。老实说,这应该是编写SAT解算器最简单的部分,但是,我应该让用户输入如下数据:

Sample Input:
1              <-- 1st number denotes the number of cases I will have
5              <-- 2nd number represents the number of variables, followed 
1 2 3              who knows how many clauses.
-1 3 4
-2 -3 5
-5 -1 2
-4 1 -2
-4 -1 -2 -3 -5

*blank line separates the different cases*
.... followed by as many cases as the user said they had
#include <sstream> //you may not have known about this

int num_cases = 0;
std::cin >> num_cases;
for(int case_num=0; case_num<num_cases; ++case_num) { //for each case       
    std::vector<std::vector<int>> variables;
    int num_variables = 0;
    std::cin >> num_variables;
    std::cin.ignore(1); //ignore the newline character, it messes with getline
    for(int var=0; var<num_variables; ++var) { //for each variable
        std::string linestr;
        std::getline(std::cin, linestr, '\n'); //read whole line into string
        std::istringstream linestream(linestr); //make a stream from the line

        int newclause = 0;
        std::vector<int> lineclauses;
        while(linestream >> newclause) //for each clause
            lineclauses.push_back(newclause); //add it to the row
        //when that fails, we read the whole line
        variables.push_back(lineclauses); //so add the row to 'variables'
    }
    //Do stuff with variables vector.
}
示例输入:

有两个独立的问题:(a)读取未知数量的输入行;(b)将给定的输入行解析为未知数量的
int
s

首先,从输入中读取行。那只是:

然后,给定
str
,我们必须将其解析为
int
s。最简单的方法是将其放入:

然后逐个读取
int
s:

int i;
while (iss >> i) {
    // do something
}
或者通过传递一对下列项,将所有这些项同时放入一个
向量中

std::vector v{std::istream_迭代器{iss},
std::istream_迭代器{};
因此,记录每行输入总和的完整示例如下:

std::string str
while (std::getline(std::cin, str)) {
    std::istringstream iss(str);
    std::vector<int> v{std::istream_iterator<int>{iss},
                       std::istream_iterator<int>{}};

    if (!v.empty()) {
        std::cout << "sum=" << std::accumulate(v.begin(), v.end(), 0) << '\n';
    }
}
std::string str
while(std::getline(std::cin,str)){
标准:istringstream iss(str);
std::vector v{std::istream_迭代器{iss},
std::istream_迭代器{};
如果(!v.empty()){

std::cout这在家庭作业和竞赛问题中非常常见,答案如下:

Sample Input:
1              <-- 1st number denotes the number of cases I will have
5              <-- 2nd number represents the number of variables, followed 
1 2 3              who knows how many clauses.
-1 3 4
-2 -3 5
-5 -1 2
-4 1 -2
-4 -1 -2 -3 -5

*blank line separates the different cases*
.... followed by as many cases as the user said they had
#include <sstream> //you may not have known about this

int num_cases = 0;
std::cin >> num_cases;
for(int case_num=0; case_num<num_cases; ++case_num) { //for each case       
    std::vector<std::vector<int>> variables;
    int num_variables = 0;
    std::cin >> num_variables;
    std::cin.ignore(1); //ignore the newline character, it messes with getline
    for(int var=0; var<num_variables; ++var) { //for each variable
        std::string linestr;
        std::getline(std::cin, linestr, '\n'); //read whole line into string
        std::istringstream linestream(linestr); //make a stream from the line

        int newclause = 0;
        std::vector<int> lineclauses;
        while(linestream >> newclause) //for each clause
            lineclauses.push_back(newclause); //add it to the row
        //when that fails, we read the whole line
        variables.push_back(lineclauses); //so add the row to 'variables'
    }
    //Do stuff with variables vector.
}
#include//您可能不知道这一点
int num_cases=0;
性病:cin>>病例数;
对于(int case\u num=0;case\u num>num\u变量;
std::cin.ignore(1);//忽略换行符,它会弄乱getline
for(int var=0;var>new子句)//对于每个子句
lineClaires.push_back(newClaires);//将其添加到行中
//如果失败了,我们就通读整行
variables.push_back(lineclauses);//因此将该行添加到“variables”中
}
//做一些关于向量变量的事情。
}

Hm.如果我正确理解OP,子句的数量是未知的?@PeterSchneider:对,第一行是案例的数量。每个案例都以一行包含“变量”的数量开始。每个“变量”都是一行包含未知数量的“子句”。这就是为什么我们必须通过
getline
将整行抓取到
istringstream
并循环它,直到它为空。为了清楚起见,我添加了注释。我不熟悉这个fomat(尽管我在这里看到它在问题中弹出);但是在给出的示例中,在5.一个疏忽?阅读
getline()
我理解它是读的(并且丢弃了)换行符;为什么是cin.ignore()`?@PeterSchneider:哦,你是对的,是
>
留下了换行符。我移动了ignore。你是对的,示例中5后面有6行,我没有解释。为什么有6个“变量”?
#include <sstream> //you may not have known about this

int num_cases = 0;
std::cin >> num_cases;
for(int case_num=0; case_num<num_cases; ++case_num) { //for each case       
    std::vector<std::vector<int>> variables;
    int num_variables = 0;
    std::cin >> num_variables;
    std::cin.ignore(1); //ignore the newline character, it messes with getline
    for(int var=0; var<num_variables; ++var) { //for each variable
        std::string linestr;
        std::getline(std::cin, linestr, '\n'); //read whole line into string
        std::istringstream linestream(linestr); //make a stream from the line

        int newclause = 0;
        std::vector<int> lineclauses;
        while(linestream >> newclause) //for each clause
            lineclauses.push_back(newclause); //add it to the row
        //when that fails, we read the whole line
        variables.push_back(lineclauses); //so add the row to 'variables'
    }
    //Do stuff with variables vector.
}