I am working on a project that includes iterating through a vector filled with pointers to class objects. What I want to do is to save pointers to 2 objects that I am looking for. I can find the objects, save them to a class pointers variables using two if loops and then... Something happens. At the end of the for loop, my variables holds reference to the end of Vector, to the last object listed there.
I guess thats because I hold pointers to iterator itself instead of specific object in my vector, but I have no idea how to fix that. I tried reating two different for loops and using break when I find the object, but when I tried, the first pointer was still the same as second one even though they weren't on the end of the vector anymore. I also tried creating function in my class(the one held in vector) that returns "this" pointer and I wanted to use it to save its address in my variables, but it didn't help.
Listing* a;
Listing* b;
for (auto it : data){
if(it.getName()==first){
a = ⁢
break;
}
}
for (auto it : data){
if(it.getName()==second){
b = ⁢
break;
}
}
I checked and I am sure that the if condition is fulfilled only once so it surely isn't "maybe it goes into if everytime" I expect a and b to hold pointers to specific objects held in vector instead of having the same/enf of vector value
a = ⁢that would the address of a temporary.auto &itwould probably solve your problem, but why not use iterators and algorithms (likestd::find, orstd::find_if, etc ? Lastly, it may be at-most once, but it also better be at-least once (so guaranteed once). Otherwiseaandbare indeterminate as shown here.