Declaring a 2D vector - c++

In some cases only the below line works.Why so?
vector< vector<int>> a(M,N);
This works in every case.
vector< vector<int>> a(M, vector<int> (N));
What's the difference?

std::vector has a fill constructor which creates a vector of n elements and fills with the value specified. a has the type std::vector<std::vector<int>> which means that it is a vector of a vector. Hence your default value to fill the vector is a vector itself, not an int. Therefore the second options is the correct one.
std::vector<std::vector<int>> array_2d(rows, std::vector<int>(cols, 0));
This creates a rows * cols 2D array where each element is 0. The default value is std::vector<int>(cols, 0) which means each row has a vector which has cols number of element, each being 0.

For declaring a 2D vector we have to first define a 1D array of size equal to number of rows of the desired 2D vector.
Let we want to create a vector of k rows and m columns
"vector<vector<int>> track(k);"
This will create a vector of size k. Then use resize method.
for (int i = 0; i < k; i++) {
track[i].resize(m);
In this way you can declare a 2D vector

Related

3 dimensional vector in c++

i want to store a vector of objects for a node and subset of set.
so I want to implement a 3 d vector where the first dimension has size #nodes = n and the second dimension has size 2^|set| = m. the last dimension shouldn't have fixed size, because I want to append new objects in my program.
vector< vector < vector<Object>>> Vector3d( n , ( m, ( ( ???)))
what should I write instead of ???
i have tried to use arrays inside of a vector, but I didn't succeed that way.
thank you in advance
The method of initialisation is
vector< vector < vector<Object>>> Vector3d(n,vector<vector<Object>(m))
This will work, as it will initialize the first dimension's size as n, which would contain n no of vector<vector<Object>(m) which is the default value. Now the size of the second dimension is defined as m, which has no initial value, hence you can append any vector to the second dimension space.
i.e.
vector<Object> v;
Vector3d[i][j].push_back(v);
where i and j are the indexes of the first 2 dimensions

How to push queue into a vector of queues when vector size is not constant? [duplicate]

In some cases only the below line works.Why so?
vector< vector<int>> a(M,N);
This works in every case.
vector< vector<int>> a(M, vector<int> (N));
What's the difference?
std::vector has a fill constructor which creates a vector of n elements and fills with the value specified. a has the type std::vector<std::vector<int>> which means that it is a vector of a vector. Hence your default value to fill the vector is a vector itself, not an int. Therefore the second options is the correct one.
std::vector<std::vector<int>> array_2d(rows, std::vector<int>(cols, 0));
This creates a rows * cols 2D array where each element is 0. The default value is std::vector<int>(cols, 0) which means each row has a vector which has cols number of element, each being 0.
For declaring a 2D vector we have to first define a 1D array of size equal to number of rows of the desired 2D vector.
Let we want to create a vector of k rows and m columns
"vector<vector<int>> track(k);"
This will create a vector of size k. Then use resize method.
for (int i = 0; i < k; i++) {
track[i].resize(m);
In this way you can declare a 2D vector

Accessing a dynamic vector of vector

Here is my code:
std::vector< std::vector<int> > v;
v.push_back(std::vector<int>(2));
v[0].push_back(10);
std::cout<<(v[0])[0];
But it prints "0" instead of 10.
I am trying to make a dynamic vector that holds a vector with fixed size.
Can someone help me visualize what's happening?
The code is buggy:
std::vector<int>(2)
makes a vector of size 2 initialized with deault constructed int (which is zero), so pushing 10 just makes a vector of size 3 w/ 10 at the end (index 2).
After reading more about push_back, here's what i understand so far and how i fixed it
std::vector< std::vector<int> > vo; //empty vector of vectors
v.push_back(std::vector<int>(2)); //insert element
v.push_back(std::vector<int>(2)); //insert element
v[0][0] = 1;
v[0][1] = 2; //allocated so it's alright to add elements
v[1][0] = 3;
v[1][1] = 4;
std::cout<<v[1][1]; //4

Initialzing a 2d vector in C++

I know that you can initialize a 1d vector with some value in the following way:
vector<int> vec(10, 100); //creates a vector of size 10 and each element = 100
Now I would like to do the same thing with a 2d vector. I know the following would give an error, because the size of columns is not specified:
vector<vector<int> > vec(10, 100);
So, is there any way to accomplish this? Also, I want to keep the column size same for each vector in the 2d vector (i.e., an nxn grid).
Or maybe I can use the "std::fill()" function in some way to accomplish this? And can this functionality be extended to an nxm grid?
vector<vector<int>> vec(10, vector<int>(10, 100));
// n m value
This will create a vector of 10 vectors of size 10 and with elements initialized to 100.

Multi-dimensional vector

How can I create a 2D vector? I know that in 2D array, I can express it like:
a[0][1]=98;
a[0][2]=95;
a[0][3]=99;
a[0][4]=910;
a[1][0]=98;
a[1][1]=989;
a[1][2]=981;
a[1][3]=987;
How can one do this using the C++ STL Vector?
vector<vector<int> > a;
If you want to define the rows and columns,
vector<vector<int> > a{{11, 2, 4}, {4, 5, 6}, {10, 8, -12}};
std::vector< std::vector< int > > a; // as Ari pointed
Using this for a growing matrix can become complex, as the system will not guarantee that all internal vectors are of the same size. Whenever you grow on the second dimension you will have to explicitly grow all vectors.
// grow twice in the first dimension
a.push_back( vector<int>() );
a.push_back( vector<int>() );
a[0].push_back( 5 ); // a[0].size() == 1, a[1].size()==0
If that is fine with you (it is not really a matrix but a vector of vectors), you should be fine. Else you will need to put extra care to keep the second dimension stable across all the vectors.
If you are planing on a fixed size matrix, then you should consider encapsulating in a class and overriding operator() instead of providing the double array syntax. Read the C++ FAQ regarding this here
std::vector< std::vector<int> > a;
//m * n is the size of the matrix
int m = 2, n = 4;
//Grow rows by m
a.resize(m);
for(int i = 0 ; i < m ; ++i)
{
//Grow Columns by n
a[i].resize(n);
}
//Now you have matrix m*n with default values
//you can use the Matrix, now
a[1][0]=98;
a[1][1]=989;
a[1][2]=981;
a[1][3]=987;
//OR
for(i = 0 ; i < m ; ++i)
{
for(int j = 0 ; j < n ; ++j)
{ //modify matrix
int x = a[i][j];
}
}
If you don't have to use vectors, you may want to try Boost.Multi_array. Here is a link to a short example.
Declaration of a matrix, for example, with 5 rows and 3 columns:
vector<vector<int> > new_matrix(5,vector<int>(3));
Another way of declaration to get the same result as above:
vector<int> Row;
One row of the matrix:
vector<Row> My_matrix;
My_matrix is a vector of rows:
My_matrix new_matrix(5,Row(3));
dribeas' suggestion is really the way to go.
Just to give a reason why you might want to go the operator() route, consider that for instance if your data is sparse you can lay it out differently to save space internally and operator() hides that internal implementation issue from your end user giving you better encapsulation and allowing you to make space or speed improving changes to the internal layout later on without breaking your interface.
Just use the following method to use 2-D vector.
int rows, columns;
// . . .
vector < vector < int > > Matrix(rows, vector< int >(columns,0));
Or
vector < vector < int > > Matrix;
Matrix.assign(rows, vector < int >(columns, 0));
// Do your stuff here...
This will create a Matrix of size rows * columns and initializes it with zeros because we are passing a zero(0) as a second argument in the constructor i.e vector < int > (columns, 0).
As Ari pointed, vector< vector< int>> is the right way to do it.
In addition to that, in such cases I always consider wrapping the inner vector (actually, whatever it represents) in a class, because complex STL structures tend to become clumsy and confusing.