1
#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];

user438383
  • 5,716
  • 8
  • 28
  • 43
Zero
  • 37
  • 6

1 Answers1

0

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.

Tharsalys
  • 318
  • 1
  • 11
  • can't I make something like this?? ```vector>vec(10,vector(10,0)); ``` this work for vector of vectors. similarly how can make everything 0 in one line? – Zero Oct 18 '21 at 07:03
  • @Zero yes that works. Unfortunately, the only way to do it with arrays is to do it in 2 lines: ```std::array, 10> arr1; arr1.fill( std::vector(10, 0 ) );``` – Tharsalys Oct 18 '21 at 07:10
  • can you please elaborate why ```array,10>arr;``` is best. I can't understand why ```vectorarr[10];``` won't allow us to use STL-algorithms. – Zero Oct 18 '21 at 07:16
  • @Zero STL-algorithms are designed to work with `iterators`, which, for most basic containers, is just a wrapper around pointers. The good news is that base pointers are considered random-access iterators by default, but for readability, consistency, and ( sometimes) memory management purposes, it's always better to use smart-wrappers around raw-pointers. In this case, `std::array` provides some utility functions that a raw array doesn't. For example, indexing with bound checking `at()`. – Tharsalys Oct 18 '21 at 07:22
  • Can you please tell me where can I learn in-depth about STL. Any courses or resources suggestions are welcomed. – Zero Oct 18 '21 at 07:25
  • @Zero The definitive list of C++ resources is covered here: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Tharsalys Oct 18 '21 at 07:27
  • Sorry to bother you again. Can you name the book which you used to learn STL; – Zero Oct 18 '21 at 07:30
  • There is no 'best' book to learn things from, what works for me may not work for you. The link I posted lists 'very good' resources with detailed descriptions of what you should expect from it given your experience level. With a language this huge, it's not wise to be looking for shortcuts. The only correct answer is: learn as much as you can from as many different, good, sources as you can. – Tharsalys Oct 18 '21 at 07:33