Re: strange behaviour of vector iterator
On Feb 24, 10:30 pm, "nicolaen...@gmail.com" <nicolaen...@gmail.com>
wrote:
hi,
i was using vector template when i found out a strange thing, i
managed to restrict the 'bug' in this code:
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
vector<int>::iterator _begin(vector<int> v){
return v.begin();
}
int main(int argc, char *argv[])
{
vector<int> v;
v.push_back(4);
vector<int>::iterator b = _begin(v);
printf("%d\n",*b);
printf("%d\n",*_begin(v));
system("PAUSE");
return EXIT_SUCCESS;
}
actually output is the following:
0
4
while i was expecting:
4
4
i can't understand what happen wrong in the assignment
The function _begin() takes the vector by value. The iterator returned
is of the vector that doesn't exist after the function call.
If you were to pass it be reference, I'm sure you would get the
expected:
4
4
:-)
Regards,
-Dhruv.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]