#include <bits/stdc .h>
using namespace std;
int main()
{
array<vector<int>,10>arr1;
arr1[0].push_back(1);
arr1[0].push_back(2);
arr1[0].push_back(3);
arr1[1].push_back(4);
arr1[1].push_back(5);
arr1[2].push_back(6);
arr1[7].push_back(100);
for(auto i:arr1){
for(auto j :i)
cout<<j<<" ";cout<<"\n";}
}
I'm creating array of vectors and pushing some values and I can't figure out how to make remaining places zeros. I have an idea , first making all vectors inside each array inside to hold zeros of size 10. and instead of using push_back , I will use at().
But i need code to make vectors inside array zeros for size 10.
output :
1 2 3
4 5
6
100
Q2)
what is difference between
array<vector<int>,10>arr;
and
vector<int> arr[10];
CodePudding user response:
You can do:
array<std::vector<int>,10>arr1;
for ( auto& vec : arr1 )
vec = std::vector<int>(10, 0);
To fill all the vectors with 0s
by default. But, you can no longer do push_back
as it will insert at the 11th
position.
Second question:
They are both equivalent ( static arrays holding pointers to dynamic arrays) . array<vector<int>,10>arr
is probably the better way, since it allows you to use the range-loop and other STL-algorithms.