用字符分隔数字
Separating numbers with characters
所以我想得到一个数字列表。所以 1,2,3-5 会说1,2,3,4,5这是我到目前为止编码的内容
cin>>num;
vec.push_back(num);
if(cin.peek() == ',')
cin.ignore();
我该如何做范围部分?
点击这里阅读更多关于cin.peek()
单击此处查看一个很棒的堆栈溢出问题
点击这里 我用这个来源来帮助你。
我假设您正在使用命名空间标准如果您需要更多澄清注释,我将在我的代码中添加更多注释。
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');
相关文章:
- 如何在 C++ 中将文件中的逗号分隔数字读取到数组中?
- 分隔整数 (C++) 的数字
- 如何从文件中读取用空格分隔的数字
- C ++正则表达式允许用点分隔的数字
- 读取以逗号分隔的数字,并将其写入以制表符分隔的文件
- C++ 从文件中读取 int,每个数字用逗号分隔
- 我如何使用CIN输入整数,每个数字都被逗号分隔
- 如何从逗号分隔的数字对中填充 2 个向量 C++.
- 为什么我可以在 C++ 中用撇号分隔数字,但不能用 C 分隔数字
- 用字符分隔数字
- 逗号分隔的数字在模拟器上正确显示,但在iPad上没有显示
- 在 cpp 中使用 getline 输入数组中的空格分隔数字
- 给定分隔成数组元素的数字,如何在 int 中重新组合数字
- 如果字符串中没有空格,如何解析带有数字和字符的字符串并分隔数字和字符
- 扫描以空格分隔的数字
- 解析逗号分隔的数字(C++)
- C++ - 如何将空格分隔的数字输入数组
- 递增由不同分隔符分隔的字符数组中的数字
- 读取空格将数字从文本文件分隔到整数数组 (C++)
- 将大数字分隔为数字c++