when insert new element to list/map, it returns the iterator pointed to the newly added element.
But if insert failed, what does list/map return? I have read the references, when failed, list/map will not throw a exception.

- 1,331
- 2
- 14
- 21
-
4It should be noted that std::list's insertion functions don't fail. It either inserts, throws an exception (std::bad_alloc for out of memory conditions), or you gave it a bad iterator and you get undefined results. – Nicol Bolas Sep 06 '11 at 07:20
-
The STL reference said **push_back** will throw the bad_alloc exception, but didn't say insert. Where can I find the words? – pengdu Sep 06 '11 at 07:35
3 Answers
map::insert
returns pair of iterator and bool value. If insertion fails bool value is set to false
.

- 33,961
- 14
- 109
- 164
-
If map have the same key already, the second of pair is set to **false**. I mean if out of memory or other errors, what does map returned? – pengdu Sep 06 '11 at 07:20
-
-
1@pengdu "I mean if out of memory or other errors" - it was not clear from question – ks1322 Sep 06 '11 at 07:24
-
Actually adding a key-value pair where the key already exists is not really a _failure_, it is considered an update. – Ray Toal Sep 06 '11 at 07:42
-
@pengdu +1 for the link. On duplicate key: C#: `ArgumentException`, Java: update, C++: ignore and return false. I was wrong to say update (that's Java's behavior), so the fact that C++ actually does not update should indicate a "failure". Funny how such a simple operation is so widely different. – Ray Toal Sep 06 '11 at 13:57
cplusplus site is very clear about what map::insert returns http://www.cplusplus.com/reference/stl/map/insert/
Return value The first version returns a pair, with its member pair::first set to an iterator pointing to either the newly inserted element or to the element that already had its same value in the map. The pair::second element in the pair is set to true if a new element was inserted or false if an element with the same value existed.
The second version returns an iterator pointing to either the newly inserted element or to the element that already had its same value in the map.
iterator is a member type, defined as a bidirectional iterator type. Dereferencing this iterator accesses the element's value, which is of type pair.

- 1,897
- 13
- 14
-
cplusplus.com is great for quick checks. But if you are going to site something use the standard. Otherwise we have the potential for incorrect references or erroneous information entering the main stream. – Martin York Sep 06 '11 at 08:25
If insert cannot allocate the memory, bad_alloc
is thrown. In all other cases insert works perfectly. link
Update: It also needs to copy the inserted object which can throw any exception.

- 7,560
- 2
- 33
- 49
-
1It also needs to copy the inserted object which can throw any exception. – UncleBens Sep 06 '11 at 15:11
-
@Tux-D: unfortunatelly the real world and the standard not the same, and we should develop softwares by the current compilers. The linked article is the vector, and it is not list or map, but the exception thrown descibed here. Would you explain a better practice? – Naszta Sep 07 '11 at 08:08
-
@Tux-D: Ok. [Here is the standard.](http://store.naszta.hu/cxx_standard.pdf) Where could I find the return value, if `insert` has error? `map` starts at page 514 in pdf / 488 in standard. – Naszta Sep 07 '11 at 14:02
-
@Tux-D: one more thing: here is [MSDN](http://msdn.microsoft.com/en-us/library/81ac0zkz(v=VS.100).aspx). There is nothing about what happened if the container cannot add the value because of end of memory. – Naszta Sep 07 '11 at 14:18
-
I am not going to argue further about it (just counterproductive). [Please update your copy of the standard](http://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents/4653479#4653479) (the 98 version is way out of data). I think the quality of your answer is correctly reflected in your current score. – Martin York Sep 07 '11 at 14:50
-
What happens on out-of-memory can be inferred from the allocator. When a new item is inserted, the map either needs to allocate memory or not (in which case out-of-memory can't happen). If it needs to allocate, then it calls `allocator::allocate` which throws std::bad_alloc on failure. - As to particular containers, there's just lots of "unless otherwise specified" clauses. `insert` has no effect if an exception occurs (container remains unmodified and valid, exception propagates). – UncleBens Sep 07 '11 at 19:45