Hi!
I am using vectors for array purposes in my program. I declared my vector as a global variable.
I'm initializing it with some values at first in main. Let's say with 3 members. And then i pass the vector to a function as a parameter and try to add to more elements inside the function. But after i return back form the function the two elements are not appended. I thought that vector should be having "pass by reference" mechanism just as arrays but i'm not sure if this is my problem. Or what could the problem be?Is there something special with vector usage?
Thanks in advance...

10-17-2002

IfYouSaySo

If it is really a global variable you shouldnt need to pass it into the function at all. If you want to pass it by reference anyway, you need to explicitly do it, ie:

void myfunction(vector<MyType>& vec)
{
// do something with vector
}

10-17-2002

master5001

Your assumption isn't stupid. However keep in mind that a vector doesn't work the same as an array. It works by allocating memory stacks to hold data. Therefore, when you don't pass a vector by reference the function will make a copy of the vector and your code will make changes to the copied (and temporary) object not the one passed into it.