Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I know if std::map insert succeeded or failed?

Tags:

c++

linux

gcc

stl

I have a map in a multithreaded app mapping a class called uuid to pointer. What I want to know if an insert operation succeeded for failed.

e.g.

_mymap.insert(hint, MyMap::value_type(entry.uuid, itemptr));

Does it throw an exception or something if it fails?

like image 868
hookenz Avatar asked Feb 24 '11 14:02

hookenz


1 Answers

The first insert member function returns a pair whose bool component returns true if an insertion was made and false if the map already contained an element whose key had an equivalent value in the ordering, and whose iterator component returns the address where a new element was inserted or where the element was already located.

To access the iterator component of a pair pr returned by this member function, use pr.first, and to dereference it, use *(pr.first). To access the bool component of a pair pr returned by this member function, use pr.second.

The second insert member function, the hint version, returns an iterator that points to the position where the new element was inserted into the map.

Source: http://msdn.microsoft.com/en-us/library/81ac0zkz(v=vs.80).aspx

like image 148
renanleandrof Avatar answered Oct 28 '22 16:10

renanleandrof