如何使用c ++跳过“,”和“空格”直到“\ n”从stdin int读取

时间:2016-06-22 06:16:58

标签: c++

我想从STDIN读取整数, 1,2,3,4-

vector<int> r;
cin >> is;
stringstream iss(is);
int n;
while(iss >> n)
{
    r.push_back(n);
}

但是在“,”之后停止读取除了拆分之外还有其他方法,只能直接读取整数。

1 个答案:

答案 0 :(得分:1)

在这里,您必须在显示here所示的每个数字之后使用并跳过,

vector<int> r;
cin >> is;
stringstream iss(is);
int n;
while(iss >> n)
{
    r.push_back(n);
    char c;
    iss >> c;
}

请参阅运行示例here