Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

map.erase( map.end() )?

Tags:

People also ask

What is map end ()?

C++ Map Library - end() Function The C++ function std::map::end() returns an iterator which points to past-the-end element in the map. The past-the-end element is the theoretical element that would follow the last element in the map.

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 delete the last element on a map?

The map is the hash table based data type, it has key and value. We can use the prev() method to get last element, and erase() function to delete as follows.

How do I delete a map key?

To delete a key from a map, we can use Go's built-in delete function. It should be noted that when we delete a key from a map, its value will also be deleted as the key-value pair is like a single entity when it comes to maps in Go.


Consider:

#include <map>  int main() {     std::map< int, int > m;     m[ 0 ] = 0;     m[ 1 ] = 1;      m.erase( 0 );  // ok     m.erase( 2 );  // no-op     m.erase( m.find( 2 ) );  // boom! } 

(OK, so the title talks abouting erasing an end() iterator, but find will return end() for a non-existent key.)

Why is erasing a non-existent key OK, yet erasing end() blows up. I couldn't see any explicit mention of this in the standard?

I've tried this on VS2005 (throws an exception in debug configuration) and GCC 4.0.1 (100% CPU). Is it implementation dependent?

Thanks.