Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

2d vector help

Posted on 2006-11-14
5
Medium Priority
?
334 Views
Last Modified: 2008-02-01
I'm hoping someone could help me with 2-d vectors.

Could someone tell me how to:

1.  perform a push_back on a 2d vector
2.  perform a resize on a 2d vector
3.  make a copy of a 2d vector

Thanks!
0
Comment
Question by:cfans
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 12

Assisted Solution

by:rajeev_devin
rajeev_devin earned 400 total points
ID: 17945020
For 1.

vector<vector<int> > matrix;

vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);

matrix.push_back(v);
0
 
LVL 9

Accepted Solution

by:
DrAske earned 400 total points
ID: 17945026
1)
vector< vector<int> > matrix(max_index); // to set the size of the array

for(int i = 0; i<matrix.size(); ++i)
    matrix[i].reserve(max_index);  // to set the size of each column .. Now the matrix is (max_index X max_index)

// set all indecies to 0
for(int x = 0 ;x<matrix.size(); ++x)
    for(int y = 0 ; y<matrix[0].size(); ++y)
           matrix[i][j] = 0;

OR:

vector<int> v(max_index);
vector< vector<int> vv(max_index);
for(int i = 0; i<v.size(); ++i)
    v.push_back(0);

vv.push_back(v);


2)

matrix.resize(max_index+1, vector<int>(max_index)); // max_index+1 is the new size of the matrix

3)
I didn't do that before, but try this

vector< vector<int> > copyMatrix = matrix;

regards,Ahmad;


0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17945030
2.
//for 10x20
vector<vector<int> > matrix;

matrix.resize(10);
for (int i = 0; i < 10; ++i)
   matrix[i].resize(20);

0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17945035
Copy is already suggested by DrAske.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 17946091
You can set both dimensions when creating the 2d array, e. g.

    vector<vector<int> > matrix(10, vector<int>(20, 0) );  // 10 x 20 all zeros

>>>> 1.  perform a push_back on a 2d vector

push_back is an operation defined for a 1d array. So if you look at the first index as the ROW index, you would add a new row by push_back.

For a 2d array push_back isn't well defined cause normally a 2d array has the same column size for all rows. A push_back for a row would add one cell to that row, what might be strange. If you want to increase the number of columns for all rows you better use the resize function for any row:

 vector<vector<int> >::iterator i;
 for (i = matrix.begin(); i != matrix.end(); ++i)
       i->resize(i->size()+1, 0);  // increment and set to 0


Regards, Alex

0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question