Solved

C++  Pass multidimensional vector  to a fucntion

Posted on 2007-11-21
6
663 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now