How to pass a vector as a parameter of a function in C++

For a unidimensional vector, you declare your function like this:

void foo( vector<int> & bar )
{
//do something
}

And this way for a bidimensional vector:

void foo( vector<vector<int> > & bar )
{
//do something
}

To pass a vector pointer to a function, you must use the & symbol.

Remember to leave the space between the two “> >”.

This is how you use the function:

vector<int> bar;

foo(bar);

More about their usage:

http://en.wikipedia.org/wiki/Vector_(C%2B%2B)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: