2d vectors

Pages: 12
Ok guys so am on 2d vectors and iterating over them... from what i've understood so far...with vector matrix there is a lil bit more freedom...like...the column of each row doesnt have to be the same like with arrays...row 1 can have have 3 columns and row 2 can have 5 columns...is that right? ok...so it means every row can have a different size...so lets say i have a 2d vector...how do i ask the size of row 5...
so lets say this i have the 2d vector holder...
std::vector<std::vector<int>>holder; lets assume that vector holder is filled as a matrix...5x5..let's assume i dont know that holder is filled 5x5...but i know is filled anyway and i want to check the size of row 5..and how many integers it contains...how do i ask for that?..
holder.size()..is surely not enough right??? that will give me the size of the whole 2d i think... guys help me out..and pls confirm if my assumptions till now are all right...or am i immagining things?

i want to check the size of row 5..and how many integers it contains...how do i ask for that?..
holder[4].size();
holder.size()..is surely not enough right??? that will give me the size of the whole 2d i think

Not exactly. holder is a vector. Each element of the vector is a row. (A "row" is, in fact, another vector, but that's not important right now.) So holder.size() will give you the number of elements in holder - i.e. the number of rows.

Cubbi's answer is the correct one for your question.

Edit: One way to make this clearer for yourself is to use typedefs, e.g.

1
2
3
typedef std::vector<int> RowType;

std::vector<RowType> holder;


This is exactly equivalent to writing:

 
std::vector<std::vector<int>> holder;


but makes it clear that holder is a single vector of rows.
Last edited on
You guys are just great!!!thanks Sir Cubbi..thanks MikeyBoy for the further explanation...you always take the trouble to explain further...Mikey..thanks again
You're welcome - glad it helped :)
OK MikeyBoy..one last thing..so from your example...just to be clear...Rowtype is column and holder is the row right?...i want to be able to visualize this thing...if i cant visualize very well i dont understand...
closed account (Dy7SLyTq)
no holder is the name of the variable.
thats right DTSCode...i mean holder[i], where i is an int starting from 0, represents my rows right? and RowType[i], will be my columns in the 2d right? :D ...
closed account (Dy7SLyTq)
yes so holder[ROW][COLUMN]
thats ok for now...thanks a lot!
Just to clarify:

holder is really the entire 2-dimensional "table". It contains all the rows, which in turn contain all the integers.

holder[i] is a single "row". It contains all the integers in that row.

holder[i][j] is a single element - that is, a single integer.

Note that calling these things "rows" and "columns" is really just a way for you as a human being to visualize them. They're just containers within containers. You could just as easily say:

1
2
3
typedef std::vector<int> ColumnType;

std::vector<ColumnType> holder;


and it would still work just the same. As long as your code treats these things consistently, then it works. In fact, this is another reason why it helps to use a typedef like I did - it helps you, and anyone else looking at your code, to understand and remember clearly what these things represent.
Last edited on
Clear Mikey, very clear...thanks again master!
You're welcome :)
sorry guys for disturbing again..am having a new king of problem...after doing somethign like this...this is an example..
1
2
3
4
5
6
std::vector<std::vector<int> > tmat= {{1,2,3,4,4,},
                                        {7,4,5,7,6,},
                                        {4,0,7,9,0,},
                                        {4,5,5,9,0,},
                                        {4,5,9,9,0,},
                                        };

the compiler gives the error...in c++ 98..tmat must be initialized by constructor not by {....} checking on the forum I found out that what i did is a c++11 syntax...thats is why the error...hmmm..so how do i do the same thing in c++98 without error???
closed account (Dy7SLyTq)
vectors arent the same as arrays. thats how you initialize arrays. with vectors you use for loops. although i think with c++11 you can use initializer_list
There are libraries for C++98 that emulate this C++11 initialization syntax (boost.assign and similar). I think they are a little awkward to use.

If you're really constrained to C++98, the most practical approach, I think, is to initialize an array and construct your vector from it:

1
2
3
4
5
6
7
8
int tmat_init[][5] = {{1,2,3,4,4,},
                      {7,4,5,7,6,},
                      {4,0,7,9,0,},
                      {4,5,5,9,0,},
                      {4,5,9,9,0,}};
std::vector< std::vector<int> > tmat;
for(int n = 0; n < 5; ++n)
    tmat.push_back(std::vector<int>(tmat_init[n], tmat_init[n]+5));
Great Sir Cubbi..working fine... but can you check this other syntax for me...
thanks...
1
2
3
4
5
6
7
8
9
//temp is a certain int value declared somewhere
//where container is a 1d vector declared somewhere

std::vector<int>::iterator itr1, itr2 = container.end();

for (itr1=container.begin(); itr1 != itr2; ++itr)
{ 
    if(*itr!==temp) container.erase(*itr);
}


just wanting to know if how am using erase() is correct...i clearly dont delete the itr pointer right? only the value in that position is deleted right? thank you
Last edited on
closed account (Dy7SLyTq)
there is no !== operator
thats right DTSCode..just typing error...can you pls respond my question? thanks
closed account (Dy7SLyTq)
sure it looks good, except i think you can omit the *. and i would use auto with a range based for loop

edit: and yes you arent erasing the iterator, just the element at the iterator

side note: why dont you just use vector::clear
Last edited on
Pages: 12