Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C++  Pass multidimensional vector  to a fucntion

Posted on 2007-11-21
6
Medium Priority
?
671 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
[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
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 500 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:Deepu Abraham
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 11

Expert Comment

by:Deepu Abraham
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
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 technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

618 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