Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,939 questions

51,876 answers

573 users

How to make calculation with the value of each element 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, double>& mp)
{
	for (auto element : mp) {
		cout << element.first << ": " << element.second << endl;
	}
	cout << endl;
}


int main()
{
	map<string, double> mp{ { "c++", 3.14 }, { "c", 5.18 } 	};

	for_each(mp.begin(), mp.end(), [](map<std::string, double>::value_type& element) {
		element.second *= 2;
	});

	printMap(mp);

	return 0;
}

/*
run:

c: 10.36
c++: 6.28

*/

 



answered Jan 10, 2018 by avibootz
...