Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why no transparent C++1x std::map::at?

Is there a reason for missing transparent (template <class K> at(K&& key);) in std::map?

like image 506
bobah Avatar asked Nov 23 '16 14:11

bobah


People also ask

Does std::map use == operator?

They won't use operator==() . The only comparison used to locate objects is the third template argument to std::map<K, V, Compare, Allocator> .

What does map Find return if nothing is found C++?

map find() function in C++ STL Return Value: The function returns an iterator or a constant iterator which refers to the position where the key is present in the map. If the key is not present in the map container, it returns an iterator or a constant iterator which refers to map. end().

What does std::map clear do?

map::clear() clear() function is used to remove all the elements from the map container and thus leaving it's size 0.

Are maps unique C++?

Each element in a map is uniquely identified by its key value. Aliased as member type map::key_type. Type of the mapped value. Each element in a map stores some data as its mapped value.


1 Answers

My guess is that std::map::at() must be a "bounds-checked" version of std::map::operator[](). Providing a transparent version of std::map::operator[]() imposes an additional requirement on std::map::key_type and the query key type K - if the query key is not in the map, it must be inserted (with default constructed value), which means that std::map::key_type must be constructible from the the query key type.

like image 155
Leon Avatar answered Nov 15 '22 12:11

Leon