在 C++ 创建一个字典

本文将介绍如何在 C++ 中创建一个字典。

使用初始化列表构造函数在 C++ 中创建字典

在 C++ 标准容器库中,有一个名为 std::map 的字典,它实现了具有唯一键的排序键值对。对 map 元素的搜索、删除和插入对等操作具有对数的复杂度。这个方法使用大括号用文字值来初始化 map 对象。注意,每个元素对都用逗号隔开,如下面的代码示例所示。

#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;
}

输出:

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

使用默认构造函数在 C++ 中创建一个字典

另外,也可以用给定的参数声明一个 map 类型的对象,然后用一个单独的语句初始化每个键值对。在这个示例中,我们演示了一个带有 int 键和 string 值的 map。我们总是可以把这些语句放在循环中,或者从用户输入中取值,这将更符合现实世界的应用场景。

#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;
}

输出:

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

使用 copy 构造函数在 C++ 中创建一个字典

另一种创建新的 map 对象的方法是使用 copy 构造函数,它将另一个现有的 map 变量作为参数,并将键值对复制到新初始化的对象中。注意,这个方法不会移动现有的 map 对象,可以在以后的程序执行过程中重复使用。

#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;
}

输出:

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

使用基于范围的构造函数在 C++ 中创建一个字典

基于范围的构造函数是前述方法的另一种选择。这个解决方案可以用来初始化一个新的 map 变量,使用一些现有 map 对象的子集键值对。在这个例子中,我们使用 find 方法来指定范围内的第一个键值对。因此,一个新的 map2 变量包含了从键值 2 开始到 map1 对象最后一个元素的键值对。

#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;
}

输出:

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