[C++] vector 实现2维数组存储输入流中以逗号和空格分隔的数据

// 做题时连输入都解决不了的菜鸡的悲伤回忆
// https://blog.csdn.net/weixin_43821376/article/details/100490943 参考方法 但是不清楚为什么能自动忽略逗号或者其他符号
// https://blog.csdn.net/weixin_39956356/article/details/118053985 这个大佬讲了一下,没细看,心情好低落


#include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<vector<int>> data2D; int tmp; int cnt = 0; vector<int> data;
while (cin >> tmp, ++cnt) { data.push_back(tmp);
  // 每行两个元素
if (cnt == 2) { data2D.push_back(data); data.clear(); cnt = 0; } if (cin.get() == \n) break; }
// 打印结果
for (auto ival2D : data2D) { for (auto ival : ival2D) cout << ival << " "; cout << endl; } cout << endl; return 0; }
输入示例:
1
,2 3,4 5,6

输出示例:
1 2 3 4 5 6

 

[C++] vector 实现2维数组存储输入流中以逗号和空格分隔的数据

上一篇:SpringBoot下使用AspectJ(CTW)下不能注入SpringIOC容器中的Bean


下一篇:05_Go语言( 流程控制)