c++ - STL vector usage problem -- Function returning non-zero Iterator -


I was trying to use vector STL, where IAM is facing a strange response from the following sample program :

#include & lt; Iostream & gt; # Include & lt; Algorithm & gt; # Include & lt; Vector & gt; using namespace std; Vector & lt; Int & gt; :: Interesting Iterator (); Of vector & lt; Int & gt; Myvector; Bull iodide (int i) {returns ((i% 2) == 1); } Int main () {vector & lt; Int & gt; :: iterator; This = funny (); If (this == myvector.end ()) cout & lt; & Lt; "Reach the end, global" & lt; & Lt; Endl; And cout & lt; & Lt; "Not the End" & lt; & Lt; Endl; } Vector & lt; Int & gt; :: Interesting Iterator () {Vector & lt; Int & gt; :: Iterator; Myvector.push_back (10); Myvector.push_back (26); Myvector.push_back (40); Myvector.push_back (56); This = search_if (myvector.begin (), myvector.end (), isodod); Cout & lt; & Lt; "The first strange value is" & lt; & Lt; * This is & lt; & Lt; Endl; If (this == myvector.end ()) cout & lt; & Lt; "Finally reached, inside the function" & lt; & Lt; Endl; And cout & lt; & Lt; "Not the End" & lt; & Lt; Endl; return it; }

The Iam function is being "finished ending" inside the fun (), while, in the main event, it is showing in the form of "non end". Not sure, what might be the reason for this, besides, it was found that, myvector.end () is shown in the form of zero in the main program [after the fun (call)) , Where-where the function is showing a non-zero value inside the fun ()

The function is using the local macro, using the main global.

Generates your revised code:

  arrived at the end, expected inside the function, global  

as expected.

Edit: Okay, not as expected - As others have said:

  this = find_if (myvector.begin ( ), Myvector.end (), isodod); Cout & lt; & Lt; "The first strange value is" & lt; & Lt; * This is & lt; & Lt; Endl;  

will cause undefined behavior with your dataset, because you do not have any strange values ​​you want:

  this = find_if (myvector.begin (), Myvector.end (), isodod); If (this! = Myvector.end ()) {cout & lt; & Lt; "The first strange value is" & lt; & Lt; * This is & lt; & Lt; Endl; }  

Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -