0

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

3
  • 1
    First there are no iterators here. Second, a = ⁢ that would the address of a temporary. auto &it would probably solve your problem, but why not use iterators and algorithms (like std::find, or std::find_if, etc ? Lastly, it may be at-most once, but it also better be at-least once (so guaranteed once). Otherwise a and b are indeterminate as shown here. Commented Jun 3, 2019 at 22:07
  • God dammit, changing to &it worked. I had to do it this way because... reasons :/ Anyway, I knew it had to be some slight overlook. Thanks a lot Commented Jun 3, 2019 at 22:18
  • Glad it helped. What a difference a little ampersand makes. Commented Jun 3, 2019 at 22:20

1 Answer 1

1

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

Yes. it doesn't exist as soon as you exit from the loop - so you're facing undefined behaviour, which means your program may do anything - usually not what you want.

Note that it isn't an iterator, it is a copy of your vector objects.

You'd want your loops to read:

for (auto& it : data){
    if(it.getName()==first){
        a = ⁢
        break;
    }
}

Better yet, you'd want to use std::find or std::find_if, they are made for that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.