48

Having a vector of vector with a fixed size,

vector<vector<int> > v(10);

I would like to initialize it so that it has in all elements a one dimensional vector with initialized value (for example 1).

I have used Boost Assign as follows

v = repeat(10,list_of(list_of(1)));

and I've got a compilation error

error: no matching function for call to ‘repeat(boost::assign_detail::generic_list<int>)’

How can that be done?

starball
  • 20,030
  • 7
  • 43
  • 238
saloua
  • 2,433
  • 4
  • 27
  • 37

4 Answers4

91

This doesn't use boost::assign but does what you need:

vector<vector<int>> v(10, vector<int>(10,1));

This creates a vector containing 10 vectors of int, each containing 10 ints.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
53

You don't need to use boost for the required behaviour. The following creates a vector of 10 vector<int>s, with each vector<int> containing 10 ints with a value of 1:

std::vector<std::vector<int>> v(10, std::vector<int>(10, 1));
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • 5
    this was the first answer posted, it's the best answer, and yet it is neither the accepted answer nor the most up-voted one. sigh. life isn't fair. – abcd Oct 17 '15 at 21:25
  • 1
    I think it's because it has got `>>` i.e `>` without space in between :p – krozaine May 22 '17 at 12:41
  • @dbliss Just looked at the edit history and it appears the OP had deleted this answer just after uploading it. Maybe he saw the other person's answer after, he had finished his and didn't want to post to the same thing which other person did(even though he was first). Anyways, it feels bad for him. – deadLock Sep 03 '20 at 13:09
6

I will just try to explain it to those new to C++. A vector of verctors mat has the advantage that you can access its elements directly at almost no cost using the [] operator..

int n(5), m(8);
vector<vector<int> > mat(n, vector<int>(m));

mat[0][0] =4; //direct assignment OR

for (int i=0;i<n;++i)
    for(int j=0;j<m;++j){
        mat[i][j] = rand() % 10;
    }

Of course this is not the only way. And if you do not add or remove elements, one can also use the native containers mat[] which are nothing more than pointers. Here's my fav way, using C++:

int n(5), m(8);
int *matrix[n];
for(int i=0;i<n;++i){
    matrix[i] = new int[m]; //allocating m elements of memory 
    for(int j=0;j<m;++j) matrix[i][j]= rand()%10;
}

This way, you don't have to use #include <vector>. Hopefully, it's clearer!

moldovean
  • 3,132
  • 33
  • 36
0
#include <vector>
#include <iostream>
using namespace std;


int main(){
    int n; cin >> n;
    vector<vector<int>> v(n);
    //populate
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            int number; cin >> number;
            v[i].push_back(number);
        }
    }
    // display
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            cout << v[i][j] << " ";
        }
        cout << endl;
    }
}

Input:

4
11 12 13 14
21 22 23 24
31 32 33 34
41 42 43 44

Output:

11 12 13 14
21 22 23 24
31 32 33 34
41 42 43 44
Frank Hou
  • 1,688
  • 1
  • 16
  • 11