c++ - 从标准输入读取数组,忽略括号和逗号

标签 c++ cin

我的代码的示例输入是:

{ 1, 2, 3, 4 }

我希望忽略大括号和逗号,并将数字读入数组。

我该怎么做?

最佳答案

嗯,这可能有效:

// Ignore all characters up to and including the open curly bracket
cin.ignore(100000, '{');

// Read the numbers into an array
int my_array[4];
unsigned int array_index = 0;
cin >> my_array[array_index];
array_index++;
cin >> my_array[array_index];
array_index++;
cin >> my_array[array_index];
array_index++;
cin >> my_array[array_index];

// Ignore all characters up to and including the newline.
cin.ignore(1000000, '\n');

您可以使用 for 循环来读入数字。

关于c++ - 从标准输入读取数组,忽略括号和逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26725035/

相关文章:

c++ - vector 订阅超出范围

c++ - Qt 信号(QueuedConnection 和 DirectConnection)

c++ - 多个 CRT 出现段错误

c++ - 如何检查来自 cin 的输入是否为 double ?

c++ - 空输入对 cin.get() 意味着什么?

c++从用户输入返回字符串或字符

c++ - C++ 中的一行用户输入赋值

c++ - 创建组合框的 9x9 网格时的警告

c++ - 抛出临时变量而不是局部变量 - 为什么?

c++ - cin.ignore() 到底做了什么?