想要使用 STL 扫描 C++ 中的二维数组
Want to scanf a 2-d array in c++ using STL
我想用 c++ stl 扫描二维数组。这就是我的做法,请告诉我它有什么问题。
int test;
scanf("%dn",&test);
VVI all_integers;
while(test--)
{
all_integers.push_back(VI(istream_iterator<int>(cin),istream_iterator<int>()));
}
示例输入:-
4
1 2 3 4 5
1 2 3 4
1 2
1
其中测试是后面的行数。
你混合了std::cin
和scanf
输入,这是故意的吗?只是std::cin >> test;
有什么问题?
无论如何,程序的问题在于您一直读取到istream_iterator<int>()
,这是"输入结束"。你正在尝试这样做 4 次。显然,输入只有一端。相反,您应该阅读到一行的末尾。
这是我的解决方案:
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
int main(){
int k;
cin >> k; cout << endl << "k = " << k << endl;
ostream_iterator<int> oi(cout, " ");
vector<vector<int> > vpi;
while(k--)
{
vpi.push_back(vector<int>(istream_iterator<int>(cin), istream_iterator<int>()));
cin.clear();
cout<<"k = "<< k <<endl;
copy(vpi[vpi.size()-1].begin(), vpi[vpi.size()-1].end(), oi);
cout<<endl;
}
}
您需要在每个向量之后使用
相关文章:
- 将值从二维数组输出到文本文件
- 在二维数组中查找最小值和最大值?
- 移动二维数组中的字符
- 如何正确填充在堆上分配的二维数组?
- 传递二维数组时出现问题
- 具有随机数的二维数组不会更改
- 如何在C++中获取二维数组中最少的一列数?
- 如何使用用户输入变量制作二维数组?
- C++ 中动态二维数组的访问冲突
- 遍历二维数组的所有子数组
- 将二维数组的所有元素插入到一维数组中
- 在函数中传递二维数组
- 为什么指针在对二维数组进行排序时无法正常工作?
- 创建 std::string 的二维数组的最佳做法
- 如何将文本文件读取到二维数组中并以 c++ 打印
- 元素在二维数组 c++ 中的出现次数
- 执行 c++ 二维数组
- C++ 中的二维数组初始化为一个值
- 在C++中初始化第一维大小未知的二维数组
- 如何在二维数组中存储值?