#include <vector>
std::vector<std::vector<T> > mat;
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
No, you're working with vector objects, so if you want to set the size beforehand, you do it with a constructor:
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
int main()
{
vector<vector<int> > items ( 5, vector<int> ( 5 ) );
int k = 0;
for ( int i = 0; i < 5; i++ ) {
for ( int j = 0; j < 5; j++ )
items[i][j] = k++;
}
for ( int i = 0; i < 5; i++ ) {
for ( int j = 0; j < 5; j++ )
cout<< setw ( 3 ) << items[i][j] <<' ';
cout<<'\n';
}
}
Alternatively, you can build the vectors dynamically:
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
int main()
{
vector<vector<int> > items;
int k = 0;
for ( int i = 0; i < 5; i++ ) {
items.push_back ( vector<int>() );
for ( int j = 0; j < 5; j++ )
items[i].push_back ( k++ );
}
for ( int i = 0; i < 5; i++ ) {
for ( int j = 0; j < 5; j++ )
cout<< setw ( 3 ) << items[i][j] <<' ';
cout<<'\n';
}
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Not that it's important or anything, but you can initialize all the elements to a specific value:
vector< vector< double > > matrix( row, vector(row, int_val));
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
>Is there not that much support for 2D or 3D vectors?
Any compiler that claims to implement C++ must support vectors of vectors for a suitable number of dimensions. The syntax can be awkward, so most people end up using wrapper libraries such as boost::multi_array rather than coding it by hand.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
I'd like to bump this old thread since 2d arrays are a huge problem for the majority of C/C++ programmers. If one needs the array to be dynamic, the nightmare is there.
This page has good explanations: http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html
But it never mentions the real problem, that is, the vector resize when using the standard notation:
my_vector[i][j].push_back(some_value); // This won't work
my_vector[i][j] = some_value; // This works
The first assigment would take care of the container population and memory handling, but it won't compile with this syntax.
The second assigment will compile and work, but won't keep track of the vector size and capacity, needing external counters to tell you the right time to resize the container.
Sadly 100% of the tutorials that I found don't use the power of vectors, i.e. memory handling, when it comes to 2d vectors. Instead, they assume one doesn't need to resize the stuff, thus rendering the container useless for this matter.
Anyone would like to discuss it a little further?
The first version doesn't work because you are trying to push onto a third, non-existent dimension. The second works, because you are storing directly to an element of the second dimension.
Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
See post #4... Second code block, lines 12-17. The outer loop populates the first dimension, the inner loop populates the second dimension.
Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
Alternatively :
const int ROW_SIZE = 10;
const int COL_SIZE = 10;
int array2d[ROW_SIZE][COL_SIZE] = {0};
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
Hialek, at least one problem is in lines 27-30 of your code:
if(c=='\n'){
items.push_back ( vector<int>() );
i++;
}
The first time through, you don't have anything in items (assuming you don't have a blank line at the start of your file). The first time youdo get a '\n' you add a first item (items[0]) and then immediately increment i so that at line 33 you're referencing items[1], which doesn't exist yet.
An approach like ravenous' will probably save you a lot of headache. Fill a single item object at a time, and when you get to a '\n' push that onto the back of your items vector and create a new item.
raptr_dflo
Practically a Master Poster
602 posts since Aug 2010
Reputation Points: 76
Solved Threads: 82