Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
GreenGeeks Web Hosting - Faster, Scalable & Eco-Friendly

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Distribute your Resume to 500+ Job Boards & Recruiters


7,786 questions

10,872 answers

573 users

How to declare, initialize and print a map in C++

2 Answers

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

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


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

	for_each(mp.begin(), mp.end(), [](const map<std::string, double>::value_type& element) {
		cout << element.first << ": " << element.second << endl;
	});

	return 0;
}

/*
run:

c: 5.18
c++: 3.14

*/

 




answered Jan 10 by avibootz
0 votes
#include <iostream>  
#include <map>
#include <string>

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 } 	};

	printMap(mp);

	return 0;
}

/*
run:

c: 5.18
c++: 3.14

*/

 




answered Jan 10 by avibootz
...