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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With