How to Use an STL Map Container in C++

Jinku Hu Feb 02, 2024
  1. Use std::map to Declare a Map Container Object in C++
  2. Use the find Member Function to Search for a Specific Element in a Map in C++
  3. Use the insert Member Function to Store an Element in a Map in C++
  4. Use the merge Member Function to Join Elements of Two Map Objects in C++
How to Use an STL Map Container in C++

This guide will show you several methods of how to use the STL map container in C++.

Use std::map to Declare a Map Container Object in C++

The std::map container implements a sorted key-value pair data structure, where keys are unique. Key values sort pair elements in ascending order by default; however, the user may optionally pass a comparison function to the std::map template. You can initialize the map object with a list of values where each element is specified in separate braces. In this case, we create a map of string pairs and then print elements of it to the cout stream. Note that each element is accessed as std::pair members in the for loop.

#include <iostream>
#include <map>

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

template <typename Map>
void printMap(Map& m) {
  for (auto& p : m) cout << p.first << " : " << p.second << ";" << endl;
  cout << endl;
}

int main() {
  std::map<string, string> m1 = {
      {"11900K", "Core i9"},
      {"11700K", "Core i7"},
      {"11600K", "Core i5"},
      {"1390P", "Xeon W"},
  };

  printMap(m1);

  return EXIT_SUCCESS;
}

Output:

11600K : Core i5;
11700K : Core i7;
11900K : Core i9;
1390P : Xeon W;

Use the find Member Function to Search for a Specific Element in a Map in C++

The find member function is useful to look up a specific key-value pair in the map container. The lookup operation has logarithmic complexity. The find function takes the reference to a key and returns the iterator to the element with the equivalent key. When no element is found with the given key, a past-the-end iterator is returned.

The following code snippet demonstrates how to initialize a new map container with the range iterators. Notice that the first iterator is retrieved using the find function. Also, it may be useful to check the returned iterator for validity.

#include <cassert>
#include <iostream>
#include <map>

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

template <typename Map>
void printMap(Map& m) {
  for (auto& p : m) cout << p.first << " : " << p.second << ";" << endl;
  cout << endl;
}

int main() {
  std::map<string, string> m1 = {
      {"11900K", "Core i9"},
      {"11700K", "Core i7"},
      {"11600K", "Core i5"},
      {"1390P", "Xeon W"},
  };

  std::map<string, string> m2(m1.find("11700K"), m1.end());
  printMap(m2);

  auto ret = m1.find("11700K");
  assert(ret != m1.end());
  std::map<string, string> m3(ret, m1.end());
  printMap(m3);

  return EXIT_SUCCESS;
}

Output:

11700K : Core i7;
11900K : Core i9;
1390P : Xeon W;

Use the insert Member Function to Store an Element in a Map in C++

You can add new elements to the existing map container using the insert member function. The insert member takes the reference to the object to be added and returns a container std::pair consisting of an iterator to the inserted element and bool value indicating the successful insertion status. When the insertion fails, the iterator points to the element that prevented the operation.

#include <iostream>
#include <map>

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

template <typename Map>
void printMap(Map& m) {
  for (auto& p : m) cout << p.first << " : " << p.second << ";" << endl;
  cout << endl;
}

int main() {
  std::map<string, string> m1 = {
      {"11900K", "Core i9"},
      {"11700K", "Core i7"},
      {"11600K", "Core i5"},
      {"1390P", "Xeon W"},
  };

  auto ret1 = m1.insert({"1390P", "Xeon W"});
  if (!ret1.second) {
    cout << "Not inserted!" << endl;
  }

  ret1 = m1.insert({"1390", "Xeon W"});
  if (!ret1.second) {
    cout << "Not inserted!" << endl;
  }
  printMap(m1);

  return EXIT_SUCCESS;
}

Output:

Not inserted!
11600K : Core i5;
11700K : Core i7;
11900K : Core i9;
1390 : Xeon W;
1390P : Xeon W;

Use the merge Member Function to Join Elements of Two Map Objects in C++

The merge member function can be utilized to join the elements from two map containers. It’s called from the map object that needs to store the combined elements and takes another map as an argument. After the operation, all pointers and references to transferred elements are valid.

#include <iostream>
#include <map>

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

template <typename Map>
void printMap(Map& m) {
  for (auto& p : m) cout << p.first << " : " << p.second << ";" << endl;
  cout << endl;
}

int main() {
  std::map<string, string> m1 = {
      {"11900K", "Core i9"},
      {"11700K", "Core i7"},
      {"11600K", "Core i5"},
      {"1390P", "Xeon W"},
  };

  std::map<string, string> m4 = {
      {"11900KF", "Core i9"}, {"11600T", "Core i5"}, {"11700K", "Core i7"}};

  m1.merge(m4);
  printMap(m1);

  return EXIT_SUCCESS;
}

Output:

11600K : Core i5;
11600T : Core i5;
11700K : Core i7;
11900K : Core i9;
11900KF : Core i9;
1390P : Xeon W;
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - C++ Map