Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a key from a C++ map

Tags:

c++

map

I would like to remove a key from a STL map. However, map.erase() doesn't do anything. How would I go about doing this

like image 386
Steffan Harris Avatar asked Apr 06 '12 04:04

Steffan Harris


People also ask

How do you delete a specific map key?

HashMap remove() Method in Java HashMap. remove() is an inbuilt method of HashMap class and is used to remove the mapping of any particular key from the map. It basically removes the values for any particular key in the Map.

How do I remove something from 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 I delete a key-value pair in maps?

Using erase() : erase() is used to erase the pair in map mentioned in argument, either its position, its value or a range of number. erase(key) : Erases the key-value pair using key mentioned in its argument.

How do you delete a key-value pair in C++?

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. Parameters: The function accepts one mandatory parameter key which specifies the key to be erased in the map container.


2 Answers

It depends entirely on how you're calling it but it sounds like you may be using the first,last option. If you are, you need to keep in mind that it erase elements starting at first, up to but excluding last. Provided you follow that rule, iterator-based removal should work fine, either as a single element or a range.

If you're erasing by key, then it should also work, assuming the key is in there of course.

The following sample code shows all three methods in action:

#include <iostream> #include <map>  int main (void) {     std::map<char,char> mymap;     std::map<char,char>::iterator it;      mymap['a'] = 'A'; mymap['b'] = 'B'; mymap['c'] = 'C';     mymap['d'] = 'D'; mymap['e'] = 'E'; mymap['f'] = 'F';     mymap['g'] = 'G'; mymap['h'] = 'H'; mymap['i'] = 'I';      it = mymap.find ('b');             // by iterator (b), leaves acdefghi.     mymap.erase (it);      it = mymap.find ('e');             // by range (e-i), leaves acd.     mymap.erase (it, mymap.end());      mymap.erase ('a');                 // by key (a), leaves cd.      mymap.erase ('z');                 // invalid key (none), leaves cd.      for (it = mymap.begin(); it != mymap.end(); it++)         std::cout << (*it).first << " => " << (*it).second << '\n';      return 0; } 

which outputs:

c => C d => D 
like image 76
paxdiablo Avatar answered Sep 28 '22 20:09

paxdiablo


You would have to find the iterator first

map.erase( ITERATOR ) ;

To make this safe, you need to make sure that ITERATOR exists, however. Par example:

#include <stdio.h>
#include <map>
using namespace std ;

int main()
{
  map<int,int> m ;
  m.insert( make_pair( 1,1 ) ) ;
  map<int,int>::iterator iter = m.find(1) ;
  if( iter != m.end() )
    m.erase( iter );
  else puts( "not found" ) ;

}
like image 21
bobobobo Avatar answered Sep 28 '22 19:09

bobobobo