|
-
July 13th, 2005, 11:18 AM
#1
Passing a vector
If I have a vector of structs in a method, example:
Code:
typedef struct {
double x;
double y;
double z;
} mystruct;
vector<mystruct> myvector;
......
myvector.push_back(...);
...
printvector(???);
How would I pass the vector to another method (by reference)?
Code:
void printvector(????){
}
-
July 13th, 2005, 11:24 AM
#2
Re: Passing a vector
 Originally Posted by amt35
How would I pass the vector to another method (by reference)?
Like this:
Code:
void printvector (const vector <mystruct> & vecMyStructures)
{
for (int nIndex = 0; nIndex < vecMyStructures.size (); ++nIndex)
std::cout << vecMyStructures [nIndex].x << std::endl;
}
Last edited by Siddhartha; July 13th, 2005 at 11:36 AM.
Reason: improved...
-
July 13th, 2005, 11:36 AM
#3
Re: Passing a vector
See Effective STL by Scott Meyers. Item 16 talks about some problems to avoid.
-
July 13th, 2005, 12:40 PM
#4
Re: Passing a vector
 Originally Posted by amt35
How would I pass the vector to another method (by reference)
Yes, that would be a good idea, since it won't create a temporary object.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|