Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing vector of vectors to function

I have a function which accepts a

vector<vector<MyClass>> 

and modifies the MyClass instances. It's been a long time since I've written any C++ and I'm having difficulty remembering what is sufficient here for passing the entire arg by reference instead of by value.

My original method signature was:

void modifyVectorOfVectors(vector<vector<MyClass> > vec) { ... }

I want to make this memory efficient so I originally changed this to:

void modifyVectorOfVectors(vector<vector<MyClass*> > vec) { ... }

Then I realized that this would mean that my vec value would still make copies of all the inner vectors. So I changed my function signature to:

void modifyVectorOfVectors(vector<vector<MyClass*> >* vec) { ... }

Is this sufficient, or do I also need to do something like:

void modifyVectorOfVectors(vector<vector<MyClass*>* >* vec) { ... }

Could someone highlight the memory differences between all of these? Thanks a lot!

like image 836
lots_of_questions Avatar asked May 25 '15 19:05

lots_of_questions


1 Answers

Simply

void modifyVectorOfVectors( vector< vector< MyClass > >& vec) { ... }
like image 162
KABoissonneault Avatar answered Oct 21 '22 04:10

KABoissonneault