Different Ways to Initialize a Map in C++ - GeeksforGeeks (2024)

Improve

Map is an associative container available in the C++ Standard Template Library(STL) that is used to store key-value pairs. Let’s see the different ways to initialize a map in C++.

  1. Initialization using assignment and subscript operator
  2. Initialization using an initializer list
  3. Initialization using an array of pairs
  4. Initialization from another map using the map.insert() method
  5. Initialization from another map using the copy constructor
  6. Initialization through a range

1. Initialization Using Assignment and Subscript Operator

One of the simplest ways of initializing a map is to use the assignment(=) and the subscript([]) operators as shown below:

Syntax:

map<string, string>New_Map;

New_Map[“5”] = “6”;

Here

  • [] is the subscript operator
  • = is the assignment operator

Below is the C++ program to implement the above approach:

C++

// C++ program to implement the

// above approach

#include <iostream>

#include <map>

using namespace std;

// Driver code

int main()

{

// Initialize map using

// default constructor

map<string, string>New_Map;

// Keep on adding key-value pairs

// using subscript([]) and

// assignment(=) operators

New_Map["Ground"] = "Grass";

New_Map["Floor"] = "Cement";

New_Map["Table"] = "Wood";

// Traverse through the map

for(auto x: New_Map)

{

cout << x.first << "->" <<

x.second <<endl;

}

return 0;

}

Output

Floor->CementGround->GrassTable->Wood

Time complexity : O(NlogN) for insertion and O(N) for traversal, where N is the number of elements in the map

Auxiliary space : O(N) to store the key-value pairs in the map

2. Initialization using an Initializer list

Another way of initializing a map is to use a predefined list of key-value pairs.

Syntax :

map<string, string>New_Map = {{key1, value1},
{key2, value2},
{key3, value3}};

Below is the C++ program to implement the above approach:

C++

// C++ program to implement

// the above approach

#include <iostream>

#include <map>

using namespace std;

// Driver code

int main()

{

// Initialize map using

// default constructor

map<string, string>New_Map;

// Adding key-value pairs

// using Initializer list

New_Map = {{"Ground", "Grass"},

{"Floor", "Cement"},

{"Table", "Wood"}};

// Traverse through the map

for(auto x: New_Map)

{

cout << x.first << "->" <<

x.second <<endl;

}

return 0;

}

Output

Floor->CementGround->GrassTable->Wood

3. Initialization Using an Array of Pairs

The map stores key-value pairs, one can store the key values using the array of pairs of the same type.

Syntax:

map<string, string>New_map(old_arr, old_arr + n);

Here, old_arr is the array of pairs from which contents will be copied into the new_map.

In the case of the vector of pairs, one can use the inbuilt iterators to copy contents from the vector of pairs Into the new map.

map<int, int>New_Map(old_vector.begin(), old_vector.end());

Here, old_vector is the vector of pairs from which contents will be copied into the new_map.

Below is the C++ program to implement the above approach:

C++

// C++ program to implement

// the above approach

#include <bits/stdc++.h>

using namespace std;

// Driver code

int main()

{

// Initialize an array of pair

// of strings

pair<string,string>old_arr[] =

{

make_pair("Ground", "Grass"),

make_pair("Floor", "Cement"),

make_pair("Table", "Wood")

};

int n = (sizeof(old_arr) /

sizeof(old_arr[0]));

// Add these key-value pairs using

// the pairs stored in the array of pairs

map<string, string>New_Map(old_arr,

old_arr + n);

// Traverse through the map

for(auto x: New_Map)

{

cout << x.first << "->" <<

x.second <<endl;

}

return 0;

}

Output

Floor->CementGround->GrassTable->Wood

4. Initialization From Another Map Using map.insert() Method

A standard way of copying elements from a map to an existing old map in C++ is using the map.insert member function.

Syntax:

map<string, string> New_Map;
New_Map.insert(old_map.begin(), old_map.end());

Here, old_map is the map from which contents will be copied into the new_map.

Below is the C++ program to implement the above approach:

C++

// C++ program to implement

// the above approach

#include <iostream>

#include <map>

using namespace std;

// Driver code

int main()

{

// Initialize an map using

// default constructor

map<string, string>old_map;

// Adding key-value pairs using

// subscript([]) and assignment(=) operators

old_map["Ground"] = "Grass";

old_map["Floor"] = "Cement";

old_map["Table"] = "Wood";

// Create a new_map where contents

// of the previous map will be

// copied using copy constructor

// and iterator provided by the map

map<string, string>New_Map;

New_Map.insert(old_map.begin(),

old_map.end());

// Traverse through the map

for(auto x: New_Map)

{

cout << x.first << "->" <<

x.second <<endl;

}

}

Output

Floor->CementGround->GrassTable->Wood

5. Initialization From Another Map Using a Copy Constructor

One way to initialize a map is to copy contents from another map one after another by using the copy constructor.

Syntax:

map<string, string>New_Map(old_map);

Here, old_map is the map from which contents will be copied into the new_map.

Below is the C++ program to implement the above approach:

C++

// C++ program to implement

// the above approach

#include <iostream>

#include <map>

using namespace std;

// Driver code

int main()

{

// Initialize a map using

// default constructor

map<string, string>old_map;

// Adding key-value pairs using

// subscript([]) and assignment(=)

// operators

old_map["Ground"] = "Grass";

old_map["Floor"] = "Cement";

old_map["Table"] = "Wood";

// Create a new_map where contents

// of the previous map will be copied

// using copy constructor

map<string, string>New_Map(old_map);

// Traverse through the unordered_map

for(auto x: New_Map)

{

cout << x.first << "->" <<

x.second <<endl;

}

return 0;

}

Output

Floor->CementGround->GrassTable->Wood

6. Initialization Through a Range

Another way of initializing a map is to initialize it through a range of key-value pairs.

Syntax:

map<string, string>New_Map(old_map.begin(), old_map.end());

Here, instead of using another map, we store any range of key-value pairs.

Below is the C++ program to implement the above approach:

C++

// C++ program to implement

// the above approach

#include <iostream>

#include <map>

using namespace std;

// Driver code

int main()

{

// Initialize a map using

// default constructor

map<string,string>old_map;

// Adding key-value pairs using

// subscript([]) and assignment(=)

// operators

old_map["Ground"] = "Grass";

old_map["Floor"] = "Cement";

old_map["Table"] = "Wood";

// Create a new_map where a range

// of key-value pairs are stored

// from old_map

map<string, string>New_Map(old_map.begin(),

old_map.end());

// Traverse through the map

for(auto x: New_Map)

{

cout << x.first << "->" <<

x.second <<endl;

}

return 0;

}

Output

Floor->CementGround->GrassTable->Wood


Last Updated : 30 Mar, 2023

Like Article

Save Article

Previous

Map in C++ Standard Template Library (STL)

Next

Inserting elements in std::map (insert, emplace and operator [])

Share your thoughts in the comments

Please Login to comment...

Different Ways to Initialize a Map in C++ - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Dong Thiel

Last Updated:

Views: 5978

Rating: 4.9 / 5 (79 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Dong Thiel

Birthday: 2001-07-14

Address: 2865 Kasha Unions, West Corrinne, AK 05708-1071

Phone: +3512198379449

Job: Design Planner

Hobby: Graffiti, Foreign language learning, Gambling, Metalworking, Rowing, Sculling, Sewing

Introduction: My name is Dong Thiel, I am a brainy, happy, tasty, lively, splendid, talented, cooperative person who loves writing and wants to share my knowledge and understanding with you.