Help adding vectors to a vector of vectors

I need to add vectors to a vector of vectors, however, I don't know in advance how many vectors i need to add.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
#include<vector>

using namespace std;

//This is just an example.

int main(){
  vector< vector<int> > vov;
  for(int i=0; i<5; ++i){
    vov.push_back(new vector<int>); //Is it like that?
  }

  return 0;
}


Thanks!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>

int main()
{
  std::vector<std::vector<int>> vov{};//empty vector of vector
  std::vector<int> singleVec{};
  vov.push_back(std::move(singleVec));//so vov contains 1 single vector

  std::vector<int>* p{new std::vector<int>{}}; // p points to an empty std::vector<int>
  vov.push_back(std::move(*p));//move the object owned by p to vov;
  delete p;//object owned by p is now in stable but undefined state, so let's delete
  p = nullptr;//and better still set p to nullptr rather than keep it dangling
  std::cout << vov.size() << "\n";//prints 2
  vov[0].resize(5);//the first vector in vov now has size 5
  std::iota(vov[0].begin(), vov[0].end(), 1);//vov[0] = {1, 2, 3, 4, 5};
  vov[1].assign(7, 4);//vov[1] now has 7 4's

  for (const auto& elemOut : vov)
  {
      for (const auto& elemInner : elemOut)
      {
          std::cout << elemInner << " ";
      }
      std::cout << "\n";
  }
}

you can google the functions if unfamiliar and do come back here if anything's still unclear
Last edited on
> Is it like that?

No, it is not like that at all; the vector of vector holds values of type vector.
In general favour value semantics over pointers to dynamically allocated objects.

This is truly asinine code; for more than one reason:
1
2
std::vector<int>* p{new std::vector<int>{}}; // p points to an empty std::vector<int>
vov.push_back(std::move(*p));//move the object owned by p to vov; 


This is the way to go:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <vector>
#include <algorithm>

void print( const std::vector< std::vector<int> >& vov )
{
    for( const auto& vec : vov )
    {
        std::cout << "[ " ;
        for( int value : vec ) std::cout << value << ' ' ;
        std::cout << "]\n" ;
    }
    std::cout << "---------\n" ;
}

int main()
{
    // create a vector of vectors initially containing three empty vectors
    std::vector< std::vector<int> > vov(3) ;
    print(vov) ;

    // push back the vector [ 6, 7, 8, 9, 1, 2, 3 ]
    vov.push_back( { 6, 7, 8, 9, 1, 2, 3 } ) ;
    print(vov) ;

    vov[2].push_back(7) ; // push back 7 to vov[2]
    vov[1] = { 9, 3, 5, 2, 9, 7 } ; // replace vov[1]
    vov.front().resize( 10U, 8 ) ; // resize vov[0]
    print(vov) ;

    std::sort( std::begin(vov), std::end(vov) ) ; // sort vov
    print(vov) ;
    
    // etc.
}

http://coliru.stacked-crooked.com/a/eca28980df60bb75
This is truly asinine code; for more than one reason:

OK, since you like to be gratuitously rude let's have the reasons as well?
@gunnerfunner
Most importantly, that code will leak memory if push_back throws an exception. It also creates an extra variable (p), causes an extra dynamic allocation, and causes an avoidable move operation on the parameter inside push_back.

Why not
vov.emplace_back();
Or, if you prefer, at the cost of an extra move (because emplace_back() is marginally more error-prone) vov.push_back({});
Last edited on
max: nicely put, thank you. The new std::vector<int> syntax is from the OP who'd indeed be well served to bear in mind your comments re exception safety and go with other, safer alternatives.

ps: for the record, I did not report any post on this thread as I can see that one has been. whoever it was I hope you did not get JLBorges booted out of this forum because, all said and done, his/her contribution to all of our programming education is immense
Last edited on
Topic archived. No new replies allowed.