Hi,
the elements that you inserted in the
add() function will be valid after the function
add() ends. It is because, the vector is a local variable of the
main() function and in the
add() function you only reference it.
For this reason, you can write all the elements to the console just after the
add() call:
#include<vector>
#include<iostream>
#include<stdio.h>
using std::vector;
void add (vector<int> & a )
{
for(int i=0;i<=9;i++)
a.push_back(i);
}
int main()
{
vector<int> a;
add(a);
for (vector<int>::iterator it = a.begin(); it != a.end(); it++)
{
printf("Element: %d\n", *it);
}
}
In the moment when the
main() function ends The
vector <int> a</int> vill stop being valid and all the elements are destroyed. It is because the
vector<int> a</int> is a local variable of the
main() function.
Hope it helps.
Best regards,
J.K.