How to remove (erase) key from a map in C++

1 Answer

0 votes
#include <iostream>  
#include <map>
#include <string>
#include <algorithm>

using std::map;
using std::string;
using std::cout;
using std::endl;

void printMap(const map<string, float>& mp)
{
	for (auto element : mp) {
		cout << element.first << ": " << element.second << endl;
	}
	cout << endl;
}


int main()
{
	typedef map<string, float> StringFloatMap;

	StringFloatMap sfmap;    

	sfmap["c++"] = 3.14;
	sfmap["c"] = 6.23;
	sfmap["java"] = 7.98;

	sfmap.erase("java");

	printMap(sfmap);

	return 0;
}

/*
run:

c: 6.23
c++: 3.14

*/

 



answered Jan 10, 2018 by avibootz

Related questions

1 answer 148 views
1 answer 265 views
1 answer 209 views
2 answers 254 views
1 answer 136 views
1 answer 175 views
1 answer 152 views
...