find a pair in a STL list where only first element is known

c++, dictionary, list, queue, stl

Solution

I think I'd use the standard `find_if` algorithm:

auto pos = std::find_if(myList.begin(), myList.end(),
                        [value](std::pair<int, otherobject> const &b) { 
                            return b.first == value; 
                        });

That gives an iterator to the element with the required value -- from there, you can copy the value, delete the value, etc., just like with any other iterator.

Problem

assumend I have a (filled) list ``` std::list<std::pair<int,otherobject>> myList; ``` and want to find() the first element within this list, where int has a specific value - how can I do that? To explain it a bit further: I want to append these pairs to the list with an int that identifies otherobject but is not unique. The order where these int/otherobject pairs arrive has to be kept. When an int is found during access to elements of this list the first occurence of that int has to be given back (and removed). Thanks!

Original source