By solving this problem
Do a function with header
vector flattens (vector const & v); who receives several vectors and concatenates them in order. For example, if the function receives Vector vector
[[1, 2, 3], [4, 5], [6, 7, 8, 9]] should return the vector
[1, 2, 3, 4, 5, 6, 7, 8, 9]
with the following code:
#include<iostream>
#include<vector>
using namespace std;
vector<int> aplana(const vector< vector<int> >& vv){
vector<int> x;
//vv[i][j]
for(int i = 0; i< int(vv.size());i++){
for(int j = 0;j < int(vv[i].size());j++){
x.push_back(vv[i][j]);
}
}
return x;
}
int main(){
vector< vector<int> >vv(3);//V ector de V ectores
vv[0].push_back(1);
vv[0].push_back(2);
vv[0].push_back(3);
vv[1].push_back(4);
vv[1].push_back(5);
vv[2].push_back(6);
vv[2].push_back(7);
vv[2].push_back(8);
vv[2].push_back(9);
vector<int> v(aplana(vv));//V ector
for(int i = 0;i <int(v.size());i++){
cout<<v[i]<<' ';
}
}
I saw the situation of using many times the function push_back()
in the code, since I do not know much about vectors and I do not know another way to assign value to the "squares" of a vector that is not one by one with push_back()
or as array (vv[0][0] = 1
).
I wanted to ask them if they could explain to me what methods exist to valorise 2 or more squares of a vector at one time.