How to find out if std :: map crashes or not?

I have a map in a multi-threaded application matching a class called uuid with a pointer. What I want to know if the insert operation failed.

eg.

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

Does this raise an exception or something if it doesn't work?

+3
source share
6 answers

insert, , , . , , - . , (.. ). , , , . , , ( bool), . , , , ++ .

()

.

... pair::first, , , , . pair::second true, false, .

:

if(mp.insert(make_pair(key, value)).second == false)
{
   cout << "Insertion failed. Key was present"
}
+12
typedef std::map<std::string, int> map;
map m;
std::pair<map::iterator,bool> result = m.insert(std::make_pair("hi", 42));

result.second ,

+8

, .

std:: map:: insert , .

std:: map:: insert , , std:: bad_alloc.

+3

, , STL, . . .

, ?

+2

- , bool true, false, , .

pr, , pr.first , * (pr.first). bool pr -, pr.second.

The second insertion member function, hint, returns an iterator that indicates where the element was inserted into the map.

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

+1
source

Using the help method, insert a pair from the same first and second that you are sure not on the map (for example, (size_t) -1 for a size map, for example). If the returned iterator has this invalid value, it was inserted if it was not found on the map. Then the iterator returns. Example: insert the pair p (2,4) into the map m ((0, 1), (2, 3), (4, 5)).

std::map<size_t, size_t>::iterator ihint (map.begin ()), k (ihint); ihint++;
std::pair<size_t, size_t> pfoo (2, (size_t) -1);
k = m.insert (ihint, pfoo);
if (k->second == (size_t) -1) {
  //the pair was inserted
}
else {
  //the pair was in the map
}
//you set the good second :
k->second = 4
-1
source

Source: https://habr.com/ru/post/1794462/


All Articles