C++ : How to Initialize a map in one line using initialzer_list ?

In this article we will discuss how to initialize a map with std::initialzer_list<T>.

Let’s create a map of string as key and int as value and initialize it with initializer_list i.e.

// Initialize a Map of string & int using initializer_list
std::map<std::string, int> mapOfMarks = {
		{"Riti",2},
		{"Jack",4}
};

Here compiler will create following std::initializer_list<T> object internally,
std::initializer_list<std::pair<const std::string, int> > = {
		{"Riti",2},
		{"Jack",4}
};

Here T is a std::pair<const std::string, int> because map store the elements internally as a pair.

Initializing a map of string & vector

// Initialize a Map of string & vector of int using initializer_list
std::map<std::string, std::vector<int> > mapOfOccur = 	{
							{ "Riti", { 3, 4, 5, 6 } },
							{ "Jack", { 1, 2, 3, 5 } }
							};

Complete Example is as follows,

Output:

#include <iostream>
#include <vector>
#include <map>
#include <list>
int main() {
	// Initialize a Map of string & int using initializer_list
	std::map<std::string, int> mapOfMarks = { { "Riti", 2 }, { "Jack", 4 } };

	for (auto entry : mapOfMarks)
		std::cout << entry.first << " :: " << entry.second << std::endl;

	std::cout << std::endl;

	// Initialize a Map of string & vector of int using initializer_list
	std::map<std::string, std::vector<int> > mapOfOccur = 	{
								{ "Riti", { 3, 4, 5, 6 } },
								{ "Jack", { 1, 2, 3, 5 } }
								};

	// Iterating over the map
	for (auto entry : mapOfOccur) {
		std::cout << entry.first << " :: ";
		for (int i : entry.second)
			std::cout << i << " , ";
		std::cout << std::endl;
	}
	return 0;
}

Output:
Jack :: 4
Riti :: 2

Jack :: 1 , 2 , 3 , 5 , 
Riti :: 3 , 4 , 5 , 6 ,

Advertisements

Initializing a member variable map in constructor with std::initializer_list

#include <iostream>
#include <vector>
#include <map>
#include <initializer_list>

class Course {
	std::map<std::string, int> mMapOfMarks;

public:
	Course(std::initializer_list<std::pair<const std::string, int> > marksMap) :
			mMapOfMarks(marksMap) {
	}
	void display() {
		for (auto entry : mMapOfMarks)
			std::cout << entry.first << " :: " << entry.second << std::endl;
		std::cout << std::endl;
	}

};

int main() {
	// Creating a Course Object and calling the constructor
	// that accepts a initializer_list
	Course mathsCourse { { "Riti", 2 }, { "Jack", 4 } };

	mathsCourse.display();
	return 0;
}

Output:
Jack :: 4
Riti :: 2

 

Do you want to Learn Modern C++ from best?

We have curated a list of Best C++ Courses, that will teach you the cutting edge Modern C++ from the absolute beginning to advanced level. It will also introduce to you the word of Smart Pointers, Move semantics, Rvalue, Lambda function, auto, Variadic template, range based for loops, Multi-threading and many other latest features of C++ i.e. from C++11 to C++20.

Check Detailed Reviews of Best Modern C++ Courses

Remember, C++ requires a lot of patience, persistence, and practice. So, start learning today.

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top