I was trying to use the vector STL, where iam facing a weird response from the following sample program :
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector<int>::iterator fun();
vector<int> myvector;
bool IsOdd (int i) {
return ((i%2)==1);
}
int main()
{
vector<int>::iterator it;
it = fun();
if (it == myvector.end() )
cout << "Reached end, Global" << endl;
else
cout << "Not end" << endl;
}
vector<int>::iterator fun() {
vector<int>::iterator it;
myvector.push_back(10);
myvector.push_back(26);
myvector.push_back(40);
myvector.push_back(56);
it = find_if (myvector.begin(), myvector.end(), IsOdd);
cout << "The first odd value is " << *it << endl;
if (it == myvector.end() )
cout << "Reached end, inside the function" << endl;
else
cout << "Not end" << endl;
return it;
}
Iam getting "Reached End" inside the function fun(), whereas, in the main program, it is showing up as "Not End".
Not sure, what might be the reason. Also, found that, the Address of myvector.end() is showing up as Zero in the main program [ after the fun() call ], where-as, showing a non-zero value inside the function fun().