std::vector

I have to pass two dimentional vector to a function.
But i am not able to do this.


It's no different than passing anything else.

Usually, though, you don't want to pass large objects such as vectors by value, instead you generally want to pass by reference. Passing by value requires a copy, which might be computationally expensive:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// pass by reference (if the funciton is going to to modify the vector)
void by_ref( vector< vector<yourtype> >& v )
{
  if(v[0][1] == foo)
    Etc();
}

// pass by const reference (if the function is not going to / shouldn't change it)
void by_const_ref( const vector< vector<yourtype> >& v )
{
  if(v[0][1] == foo)
    Etc();
}

//-----------------------------------
//  alternatively you can typedef your vector to improve clarity and reduce typing:

typedef vector< vector<yourtype> >   vector2d;

   // these functions are the same as the above, just using the typedef
void by_ref(vector2d& v) { }  
void by_const_ref(const vector2d& v) { }
Thank you for the fast reply.

Also,

How can we call this function, void by_ref( vector< vector<yourtype> >& v )

Just like any other function...

by_ref(my2dvector);
Thank You :)
Topic archived. No new replies allowed.