我的代码的示例输入是:
{ 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/