function - How to return a valid iterator in C++? -
I am writing a function to find the element in vector and return an element to that element:
vector & lt; Int & gt; :: errator circular (vector & lt; int & gt; ivec, int target) {auto it = ivec.begin (); While (this! = Ivec.end ()) {If (* this = target) it returns; Other ++; } Return ivec.end (); } Then I copy the return itator to another iterator. I can correctly determine the iterator:
vector & lt; Int & gt; Number {1,2,3,4,5,6,7,8,9,10}; Auto ether = sediment (number, 5); The court's & lt; & Lt; * ITER; Output is 5. But, when I increment / decrease the increment, then something is wrong:
- iter; The court's & lt; & Lt; * ITER; // I think the output should be 4 but the output is 134281917! Is the price for search wrong?
if (* this = goal) return it; Assigns this target to * it . Since target is 5 , the expression value is also 5 , which is equal to true . Thus, with a nonzero target , your function will specify the first element of target to ivec and return to ivec.begin ( ) . It should be clear that results in undefined behavior that is repeated. Note that the quarrel with both G ++ and -white (it also emits a warning). You should always compile your code in the high alert settings.
Comments
Post a Comment