What is a hint?

In cppreference about map::emplace_hint():

template <class... Args>
iterator emplace_hint( const_iterator hint, Args&&... args );

Inserts a new element into the container, using hintas a suggestion where the element should go.

So, if this sentence means that implementation does not need to place an element there? What is a hint?

+4
source share
3 answers

Usually inserts in a tree map (e.g. std::map) require an O (log n) search to find the correct insertion point. If the correct insertion point is adjacent to the provided hint, you can save the search O (log n), and the operation O (1).

, , . .find() ( .end(), ), .lower_bound() ( , ), , , .

+8

cppreference map::emplace_hint(). , , - ++ 11 :

N3225, 102:

... .

, LWG 1253, ++ 11 :

... p.

, , p

"" . . , (map, multimap, set, multiset).

++ 98/03" " insert. N1780, emplace_hint.

- . . , . .

- cppreference :

.

+10

std::map - , -, . O (logN) - , . , , , - , .

+1

All Articles