Solved

C++  Pass multidimensional vector  to a fucntion

Posted on 2007-11-21
6
660 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

760 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

20 Experts available now in Live!

Get 1:1 Help Now