How to initialize an unordered_map using initialize list in C++

1 Answer

0 votes
#include <iostream>
#include <unordered_map>
#include <string>

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

int main() 
{
	std::unordered_map<string, int> workers({{ "Liam ", 45}, { "Logan", 37}, { "Ethan", 51}});

	for (std::pair<std::string, int> element : workers)
		cout << element.first << " - " << element.second << endl;

	return 0;
}

/*
run:

Liam  - 45
Logan - 37
Ethan - 51

*/

 



answered Feb 15, 2018 by avibootz

Related questions

3 answers 296 views
296 views asked Feb 20, 2019 by avibootz
1 answer 140 views
2 answers 165 views
1 answer 132 views
132 views asked Sep 17, 2022 by avibootz
2 answers 1,709 views
3 answers 343 views
...