所以我想要一个号码列表。所以,
cin>>num;
vec.push_back(num);
if(cin.peek() == ',')
cin.ignore();
我该怎么做靶场呢?
发布于 2017-11-01 04:34:34
单击此处阅读有关cin.peek()的更多信息
一个重大堆栈溢出问题的单击此处
单击此处,我用这个来源来帮助你。
我假设您使用的是命名空间std,如果您需要更多的说明注释,我将向我的代码中添加更多注释。
cin>>ws; //eats up white spaces
cout.flush();
do //loop to check every number
{
cin>>num1;
num_vec.push_back(num1);
if(cin.peek() == ',')
{
cin.ignore();
}
else if(cin.peek() == '-')
{
cin.ignore();
//if it sees a dash it will ignore the dash
// similar to what you did with your comma
cin>>num2;
for(++num1; num1<=num2; num1++)
{
num_vec.push_back(num1);
//keeps adding 1 to that range and pushing it back to vector
}
if(cin.peek() == ',')
{
cin.ignore();
}
}
}while(cin.peek() != '\n');
https://stackoverflow.com/questions/47048257
复制相似问题