Solved

C++  Pass multidimensional vector  to a fucntion

Posted on 2007-11-21
6
667 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

729 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