Solved

C++  Pass multidimensional vector  to a fucntion

Posted on 2007-11-21
6
665 Views
Last Modified: 2010-04-21
In visual C++, I have created a multidimensional vector.  vector<vector<int> > vI(10, vector<int>(3));
I would like to know how to pass this vector to the function below.
=========================================================
void display (vector<vector<int> >& vy)       //vy is instance of int vector
{  for (int i = 0; i < vy.size(); i++)           // loops through each row of vy
   {  for (int j = 0; j < vy[i].size(); j++) // loops through each element of each row
          cout << vy[i][j] << " ";           // prints the jth element of the ith row
      cout << endl;
   }
}
0
Comment
Question by:luling1972
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 20332527
Well, just use

void display (vector<vector<int> >& vy)       //vy is instance of int vector
{  for (int i = 0; i < vy.size(); i++)           // loops through each row of vy
   {  for (int j = 0; j < vy[i].size(); j++) // loops through each element of each row
          cout << vy[i][j] << " ";           // prints the jth element of the ith row
      cout << endl;
   }
}

voif foo()
{
vector<vector<int> > vy;

// fill vector

display(vy);

}
0
 
LVL 7

Accepted Solution

by:
lucky_james earned 125 total points
ID: 20332900
pass it like any other data structure you use.

By using reference :

sign:
void display (vector<vector<int> >& vy)
Call:
display(vy);

By using pointers:
void display (vector<vector<int> >* vy)
Call:
display(&vy);


Hope it helps.
0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 20333210
Typedef will make it more clear too.
Have a look at this:
http://www.dcs.bbk.ac.uk/~roger/cpp/week7.htm
Best Regards,
DeepuAbrahamK
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 20333225
Well, looks like your problem is also from this site :P
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 20333716
> Well, looks like your problem is also from this site :P

Good old Google. 8-)
0
 

Author Closing Comment

by:luling1972
ID: 31410515
This solution solved my problem completely.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

790 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