#include<iostream>
#include<vector>
using namespace std;
vector<vector<int>>construct2DArray(vector<int>& original,int m,int n){
vector<vector<int>>ans;
if(original.size() != m * n){
return ans;
}
//auto 这里可以代替 iteration
for(auto it = original.begin();it != original.end();it += n){
// emplace_back;
ans.emplace_back(it,it+n);
}
return ans;
}
int main(){
vector<int> arr = {1,2,3,4};
vector<vector<int>>arr1 = construct2DArray(arr,2,2);
for(int i = 0;i < 2;i++){
for(int j = 0;j < 2;j++){
cout << arr1[i][j] << " ";
}
cout << endl;
}
return 0;
}
auto 用法 参考 https://www.zhihu.com/question/35517805
emplace_back() 参考 c++11 之emplace_back 与 push_back的区别_学之知之的博客-CSDN博客_emplace_back