I'm having trouble using a function call to assign values to an empty vector of pointers. I can use a function to assign values to an ordinary array of pointers, but when I try to do the same using a vector, the assignment fails.
In the following code, the call to the vector function assigns values within the function scope, but outside the scope (i.e. in "main") the vector remains empty.
The output of this code is:
Size in function: 5
Size in main: 0
What am I missing?
Code:#include <iostream> #include <vector> using namespace std; void fillvector(vector<int*> v); int main() { int i; vector<int*> intvector; //try to fill vector with values fillvector(intvector); //check the result cout << "\nSize in main: " << intvector.size() << flush; return 0; } void fillvector(vector<int*> v) { int i; int* ptr; for(i = 0; i < 5; i++) { ptr = new int(i+1); v.push_back(ptr); } cout << "\nSize in function: " << v.size() << flush; }