Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New std::map::erase() signature C++17

According to this answer, an iterator must be implicitly convertible to const_iterator. Since that is true, as we can see happening in insert_or_assign(), then why in C++17 was a new signature added to std::map::erase()?

In C++11, we have iterator erase( const_iterator pos );

In C++17, we now have iterator erase( iterator pos );

Wasn't the C++11 signature good enough to receive iterator and const_iterator?

like image 617
João Paulo Avatar asked Jun 08 '18 16:06

João Paulo


People also ask

How do you delete a map in C++?

map::clear() function is an inbuilt function in C++ STL, which is defined in header file. clear() is used to remove all the content from the associated map container. This function removes all the values and makes the size of the container as 0.

How do you remove items from std::map?

map erase() function in C++ STL map::erase() is a built-in function in C++ STL which is used to erase element from the container. It can be used to erase keys, elements at any specified position or a given range.

Does map erase deallocate memory?

No it doesn't free the memory if it is a naked pointer. You need to ensure that the memory is deallocated appropriately.

What is the complexity of std::map :: insert () method?

Time complexity: k*log(n) where n is size of map, k is no. of elements inserted.


1 Answers

There's a potential ambiguity with erase(const key_type& key) when you pass an iterator. Consider the case where the key_type is something like std::any.

like image 185
T.C. Avatar answered Oct 02 '22 16:10

T.C.