Multidimensional Vectors
I was wondering how one would go about initializing a 2D vector.
With a regular vector, one would typically just initialize the vector in the following manner.
1 2 3 4 5 6 7
|
std::vector<int> numbers;
numbers.push_back(1);
numbers.push_back(2);
numbers.push_back(3);
|
The way I tried initialing a 2D vector was through the following:
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 36 37 38 39 40 41 42 43 44 45
|
#include <iostream>
#include <vector>
using std::vector;
int main()
{
vector<vector<int>> Numbers(3, vector<int>(4));
vector<int> Elements;
Elements.push_back(4);
Elements.push_back(3);
Elements.push_back(2);
Elements.push_back(1);
Elements.push_back(5);
Elements.push_back(6);
Elements.push_back(7);
Elements.push_back(8);
Elements.push_back(9);
Elements.push_back(10);
Elements.push_back(11);
Elements.push_back(12);
Numbers.push_back(Elements);
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 4; j++)
{
std::cout << Numbers[i][j] << std::endl;
}
}
system("Pause");
return 0;
}
|
The problem is,the output is all zeroes.
How does one initialize 2D vectors?
The problem you're having is you are setting the size and then adding to it.
If you now what is going to be in the vector of vectors before hand than here is one way.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<vector<int>> v = {{1,2,3}, {4,5,6}, {7,8,9}};
for(const auto& i : v)
{
for(const auto j : i)
{
cout << j << ' ';
}
cout << endl;
}
}
|
Or in your code just dont set the size before hand.
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 36 37 38 39
|
#include <iostream>
#include <vector>
using std::vector;
int main()
{
vector<vector<int>> Numbers;
vector<int> Elements;
Elements.push_back(4);
Elements.push_back(3);
Elements.push_back(2);
Elements.push_back(1);
Elements.push_back(5);
Elements.push_back(6);
Elements.push_back(7);
Elements.push_back(8);
Elements.push_back(9);
Elements.push_back(10);
Elements.push_back(11);
Elements.push_back(12);
Numbers.push_back(Elements);
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 4; j++)
{
std::cout << Numbers[i][j] << std::endl;
}
}
}
|
Question:
I get an error informing me my vector subscript is out of range, with the following code
1 2 3 4 5 6 7 8 9 10 11
|
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 4; j++)
{
std::cout << Numbers[i][j] << std::endl;
}
}
|
but i used the following the following and it runs fine.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
const int row = Numbers.size();
const int col = Numbers[0].size();
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
std::cout << Numbers[i][j] << std::endl;
}
}
|
The value assigned at row is one. Shouldn't it be 3?
Why is Numbers[0].size() 12?
Is this all based on how a multidimensional vector is structured internally?
I keeping getting an error with
vector<vector<int>> v = {{1,2,3}, {4,5,6}, {7,8,9}};
Is this not allowed in C++
Last edited on
> I keeping getting an error with
>
vector<vector<int>> v = {{1,2,3}, {4,5,6}, {7,8,9}};
> Is this not allowed in C++
It was not allowed in legacy C++ (C++ as it existed before 2011).
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 36 37 38
|
#include <iostream>
#include <vector>
void fn_cplus_plus_curent()
{
// http://www.stroustrup.com/C++11FAQ.html#init-list
std::vector< std::vector<int> > vec { {1,2,3}, {4,5,6}, {7,8,9} };
for( const auto& row : vec ) // http://www.stroustrup.com/C++11FAQ.html#for
{
for( int v : row ) std::cout << v << ' ' ;
std::cout << '\n' ;
}
std::cout << '\n' ;
}
void fn_cplus_plus_old()
{
std::vector< std::vector<int> > vec( 3, std::vector<int>(3) ) ; // 3x3, all zeroes
int value = 0 ;
for( std::size_t r = 0 ; r < vec.size() ; ++r )
for( std::size_t c = 0 ; c < vec[r].size() ; ++c )
vec[r][c] = ++value ;
for( std::size_t r = 0 ; r < vec.size() ; ++r )
{
for( std::size_t c = 0 ; c < vec[r].size() ; ++c ) std::cout << vec[r][c] << ' ' ;
std::cout << '\n' ;
}
std::cout << '\n' ;
}
int main()
{
fn_cplus_plus_curent() ;
fn_cplus_plus_old() ;
}
|
Topic archived. No new replies allowed.