Create a Dictionary in C++

  1. Use Initializer List Constructor to Create a Dictionary in C++
  2. Use Default Constructor to Create a Dictionary in C++
  3. Use the copy Constructor to Create a Dictionary in C++
  4. Use the Range-based Constructor to Create a Dictionary in C++

This article will introduce how to create a dictionary in C++.

Use Initializer List Constructor to Create a Dictionary in C++

In C++ standard containers library, a dictionary is named std::map, which implements sorted key-value pairs with unique keys. Operations on the map elements like search, remove, and insert pairs have logarithmic complexity. This method uses curly brackets to initialize the map object with literal values. Note that each element pair is separated by the comma, as shown in the following code example.

#include <iostream>
#include <map>

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

template<typename Map>
void PrintMap(Map& m)
{
    cout << "[ ";
    for (auto &item : m) {
        cout << item.first << ":" << item.second << " ";
    }
    cout << "]\n";
}

int main() {
    map<int, string> map1 = {{1, "Apple",},
                                {2, "Banana",},
                                {3, "Mango",},
                                {4, "Raspberry",},
                                {5, "Blackberry",},
                                {6, "Cocoa",}};

    cout << "map1 - ";
    PrintMap(map1);
    cout << endl;

    return EXIT_SUCCESS;
}

Output:

map1 - [ 1:Apple 2:Banana 3:Mango 4:Raspberry 5:Blackberry 6:Cocoa ]

Use Default Constructor to Create a Dictionary in C++

Alternatively, we can declare a map type object with given parameters and then initialize each key-value pair using a separate statement. In this sample, we demonstrate a map with int keys and the values of string-s. We can always put these statements in the loop or take values from the user input, which would better match a real-world application scenario.

#include <iostream>
#include <map>

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

template<typename Map>
void PrintMap(Map& m)
{
    cout << "[ ";
    for (auto &item : m) {
        cout << item.first << ":" << item.second << " ";
    }
    cout << "]\n";
}

int main() {
    map<int, string> map2;
    map2[1] = "Banana";
    map2[2] = "Mango";
    map2[3] = "Cocoa";
    map2[4] = "Raspberry";

    cout << "map2 - ";
    PrintMap(map2);
    cout << endl;

    return EXIT_SUCCESS;
}

Output:

map2 - [ 1:Banana 2:Mango 3:Cocoa 4:Raspberry ]

Use the copy Constructor to Create a Dictionary in C++

Another solution to create a new map object is to use a copy constructor, which takes another existing map variable as an argument and copies the key-value pairs to the newly initialized object. Notice that this method does not move the existing map object and can be reused later during the program execution.

#include <iostream>
#include <map>

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

template<typename Map>
void PrintMap(Map& m)
{
    cout << "[ ";
    for (auto &item : m) {
        cout << item.first << ":" << item.second << " ";
    }
    cout << "]\n";
}

int main() {
    map<int, string> map1 = {{1, "Apple",},
                                {2, "Banana",},
                                {3, "Mango",},
                                {4, "Raspberry",},
                                {5, "Blackberry",},
                                {6, "Cocoa",}};

    map<int, string> map3(map1);

    cout << "map3 - ";
    PrintMap(map3);
    cout << endl;

    return EXIT_SUCCESS;
}

Output:

map3 - [ 1:Apple 2:Banana 3:Mango 4:Raspberry 5:Blackberry 6:Cocoa ]

Use the Range-based Constructor to Create a Dictionary in C++

The range-based constructor is another alternative to the previous methods. This solution can be utilized to initialize a new map variable with the subset key-value pairs of some existing map object. In this example, we use the find method to specify the first key-value pair in the range. Consequently, a new map2 variable contains pairs starting from the key-value 2 to the map1 object’s last element.

#include <iostream>
#include <map>

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

template<typename Map>
void PrintMap(Map& m)
{
    cout << "[ ";
    for (auto &item : m) {
        cout << item.first << ":" << item.second << " ";
    }
    cout << "]\n";
}

int main() {
    map<int, string> map1 = {{1, "Apple",},
                                {2, "Banana",},
                                {3, "Mango",},
                                {4, "Raspberry",},
                                {5, "Blackberry",},
                                {6, "Cocoa",}};

    map<int, string> map4(map1.find(2), map1.end());

    cout << "map4 - ";
    PrintMap(map4);
    cout << endl;

    return EXIT_SUCCESS;
}

Output:

map4 - [ 2:Banana 3:Mango 4:Raspberry 5:Blackberry 6:Cocoa ]