我将2dim数组复制到2dim向量中并对其进行了一些修改,现在我想知道如何将2dim向量复制到2dim数组中?(我做如下:)
将2dim数组复制到2dim向量:
vector< vector<int>> path2;
vector<int> temp; // For simplicity
for (int x = 0; x <= 1; x++)
{temp.clear();
for (int y = 0; y < 4; y++)
{
temp.push_back(path1[x][y]);
}
path2.push_back(temp);
}
out put:
path2[0][6,0,2,6]
path2[1][6,1,3,6]
将2dim向量复制到2dim数组
int arr [4][10] ;
copy(path3.begin(), path3.end(), arr);
打印arr
for (int i=0;i< ???? ;i++)// how to define size of the vector first dimention which is 2 ( i am aware about size() for 1 dim vector, but for 2dim vector ...... ??????????
for(int j=0;j<?????; j++) //same prolem as above
cout<<endl<<arr[i][j];
问题是我不确定复制部分,而且我不知道如何定义每个矢量大小的大小?
答案 0 :(得分:4)
你可以做任何这些迭代矢量矢量:
//Works for C++11
std::vector<std::vector<int>> vec;
for(auto &i : vec)
for(auto &j : i)
std::cout << j << std::endl;
//Works for C++11
for (auto iter = vec.cbegin(); iter != vec.cend(); ++iter)
for(auto sub_iter = iter->cbegin(); sub_iter != iter->cend(); ++sub_iter)
std::cout << *sub_iter << std::endl;
//Works for C++03 and C++11
typedef std::vector<std::vector<int> >::iterator Iter;
typedef std::vector<int>::iterator Sub_Iter;
for (Iter iter = vec.begin(); iter != vec.end(); ++iter)
for(Sub_Iter sub_iter = iter->begin(); sub_iter != iter->end(); ++sub_iter)
std::cout << *sub_iter << std::endl;
//works for C++03 and C++11
for(int i = 0; i<vec.size(); ++i)
for(int j = 0; j < vec[i].size(); ++j)
std::cout << vec[i][j] << std::endl;