How to rename key in a map using 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["python"] = sfmap["java"];
	sfmap.erase("java");

	printMap(sfmap);

	return 0;
}

/*
run:

c: 6.23
c++: 3.14
python: 7.98

*/

 



answered Jan 10, 2018 by avibootz

Related questions

1 answer 223 views
1 answer 94 views
1 answer 148 views
2 answers 175 views
175 views asked Apr 13, 2020 by avibootz
1 answer 186 views
1 answer 159 views
159 views asked May 5, 2018 by avibootz
...