C++ unordered_map的用法

在这里插入图片描述

unordered_map 在 C++ 中是一个非常有用的容器,它允许你存储键值对,并且提供了基于键的快速查找。以下是一些 unordered_map 的用法举例:

  1. 基本操作
    插入元素
#include <iostream>  
#include <unordered_map>  
#include <string>  
  
int main() {  
    std::unordered_map<std::string, int> myMap;  
    myMap["apple"] = 1;  
    myMap["banana"] = 2;  
    myMap["cherry"] = 3;  
  
    // 使用 insert 函数插入  
    myMap.insert(std::pair<std::string, int>("date", 4));  
    // 或者使用 make_pair  
    myMap.insert(std::make_pair("fig", 5));  
  
    return 0;  
}

查找元素

if (myMap.find("banana") != myMap.end()) {  
    std::cout << "Found banana with value: " << myMap["banana"] << std::endl;  
} else {  
    std::cout << "Banana not found" << std::endl;  
}

访问元素

int value = myMap["apple"]; // 直接使用键访问值  
std::cout << "Value of apple: " << value << std::endl;

删除元素

myMap.erase("cherry"); // 根据键删除元素

遍历元素

for (const auto& pair : myMap) {  
    std::cout << "Key: " << pair.first << ", Value: " << pair.second << std::endl;  
}
  1. 使用自定义类型和哈希函数
    如果你想要使用自定义类型作为 unordered_map 的键,你需要为该类型提供一个哈希函数。以下是一个使用自定义类型和哈希函数的例子:
#include <iostream>  
#include <unordered_map>  
#include <functional> // for hash  
  
struct Person {  
    std::string name;  
    int age;  
  
    bool operator==(const Person& other) const {  
        return name == other.name && age == other.age;  
    }  
};  
  
namespace std {  
    template<>  
    struct hash<Person> {  
        size_t operator()(const Person& p) const {  
            size_t h1 = hash<string>()(p.name);  
            size_t h2 = hash<int>()(p.age);  
            return h1 ^ (h2 << 1); // 合并哈希值  
        }  
    };  
}  
  
int main() {  
    std::unordered_map<Person, std::string> people;  
    people[Person{"Alice", 30}] = "Software Engineer";  
    people[Person{"Bob", 25}] = "Data Scientist";  
  
    for (const auto& entry : people) {  
        std::cout << "Name: " << entry.first.name << ", Age: " << entry.first.age  
                  << ", Occupation: " << entry.second << std::endl;  
    }  
  
    return 0;  
}

在这个例子中,我们定义了一个 Person 结构体,并为它提供了一个哈希函数。这个哈希函数结合了 name 和 age 成员的哈希值来生成 Person 的唯一哈希值。然后,我们可以使用这个自定义类型作为 unordered_map 的键。

  1. 使用 Lambda 表达式作为查找条件
    你还可以使用 find_if 算法和 lambda 表达式来基于更复杂的条件查找元素:
#include <iostream>  
#include <unordered_map>  
#include <algorithm>  
  
int main() {  
    std::unordered_map<std::string, int> myMap;  
    myMap["apple"] = 1;  
    myMap["banana"] = 2;  
    myMap["cherry"] = 3;  
  
    // 使用 find_if 和 lambda 查找值大于 2 的元素  
    auto it = std::find_if(myMap.begin(), myMap.end(), [](const std::pair<std::string, int>& pair) {  
        return pair.second > 2;  
    });  
  
    if (it != myMap.end()) {  
        std::cout << "Found an element with value greater than 2: " << it->first << ",

相关推荐

  1. nc

    2024-04-07 15:48:03       63 阅读
  2. QueryWrapper

    2024-04-07 15:48:03       30 阅读
  3. axios

    2024-04-07 15:48:03       33 阅读
  4. React <> </>

    2024-04-07 15:48:03       32 阅读
  5. pymysql基本

    2024-04-07 15:48:03       60 阅读
  6. css_auto

    2024-04-07 15:48:03       56 阅读
  7. 关于QUOTENAME

    2024-04-07 15:48:03       60 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-07 15:48:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-07 15:48:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-07 15:48:03       82 阅读
  4. Python语言-面向对象

    2024-04-07 15:48:03       91 阅读

热门阅读

  1. GraphQL入门教程:构建更高效的APIs

    2024-04-07 15:48:03       36 阅读
  2. C++之eigen库学习

    2024-04-07 15:48:03       39 阅读
  3. 阿里+++

    阿里+++

    2024-04-07 15:48:03      33 阅读
  4. SpringBoot学习笔记

    2024-04-07 15:48:03       33 阅读
  5. Qt文本搜索

    2024-04-07 15:48:03       34 阅读
  6. 如何设置redis集群

    2024-04-07 15:48:03       35 阅读
  7. 前端面试经验

    2024-04-07 15:48:03       40 阅读
  8. 训练营十四天(二叉树的遍历)

    2024-04-07 15:48:03       142 阅读
  9. 1688详情、搜索、店铺、图搜

    2024-04-07 15:48:03       39 阅读