C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > C++ std::map insert函数

C++ 中 std::map的insert函数使用案例

作者:青草地溪水旁

C++中std::map::insert用于安全插入键值对,检查键是否存在避免覆盖,返回迭代器和布尔值,支持多种重载形式,键唯一,时间复杂度O(logn),需注意返回值判断,本文给大家介绍C++ 中 std::map的insert函数使用案例,感兴趣的朋友一起看看吧

1. 函数的概念与用途

std::map::insert 是 C++ 标准模板库(STL)中 map 容器的一个核心成员函数。它的核心任务很明确:map 中插入一个新的键值对(key-value pair)

核心用途:

简单来说,insert 是一个“安全”的插入方式,它不会意外地覆盖你已经存在的数据。

2. 函数的声明与出处

std::map 及其 insert 函数定义在 <map> 头文件中,属于 C++ 标准库,因此不需要额外链接库,只需包含头文件即可。

它有多个重载版本,最常用的一种声明如下:

#include <map>
std::pair<iterator, bool> insert(const value_type& value);

3. 返回值的含义与取值范围

这是 insert 函数非常关键的一部分。它的返回值是一个 std::pair,包含两个成员:

通过检查 second 成员,你可以立即知道插入操作是否成功。

4. 参数的含义与取值范围

最常用的重载版本参数是 const value_type& value

其他常见重载:

5. 函数使用案例

下面是一个典型的代码示例,演示了如何插入、如何检查返回值以及如何避免重复插入。

#include <iostream>
#include <map>
#include <string>
int main() {
    std::map<int, std::string> studentMap;
    // 方式一:直接用 pair 插入
    auto ret1 = studentMap.insert(std::pair<const int, std::string>(1, "Alice"));
    if (ret1.second) {
        std::cout << "Inserted student: (" << ret1.first->first << ", " << ret1.first->second << ")\n";
    }
    // 方式二:更现代的方法,使用 make_pair 或 {}
    auto ret2 = studentMap.insert({2, "Bob"});
    if (ret2.second) {
        std::cout << "Inserted student: (" << ret2.first->first << ", " << ret2.first->second << ")\n";
    }
    // 尝试插入一个重复的键
    auto ret3 = studentMap.insert({1, "Charlie"}); // 键 1 已存在
    if (!ret3.second) {
        std::cout << "Insertion failed. Key " << 1 << " already exists with value: " << ret3.first->second << "\n";
    }
    // 使用 C++17 的结构化绑定 (Structured Binding) 来简化返回值处理
    auto [iterator, success] = studentMap.insert({3, "David"});
    if (success) {
        std::cout << "Inserted student: (" << iterator->first << ", " << iterator->second << ")\n";
    }
    // 打印整个 map
    std::cout << "\nFinal map contents:\n";
    for (const auto& [id, name] : studentMap) {
        std::cout << id << " => " << name << '\n';
    }
    return 0;
}

6. 编译方式与注意事项

编译命令(使用 GCC):

g++ -std=c++17 -o map_insert_demo map_insert_demo.cpp

注意事项:

  1. 键的唯一性map 的键是唯一的。insert 不会覆盖已存在的键对应的值。如果你想要覆盖,应该使用 map[key] = value;
  2. 性能:插入操作的时间复杂度为 O(log n),因为 map 底层通常是红黑树实现。
  3. 返回值务必检查:如果你需要知道插入是否成功,一定要检查返回值的 second 成员。忽略返回值可能会导致你误以为插入成功了。
  4. C++11 及以上:推荐使用花括号 {} 来创建 pair 对象,代码更简洁(如 {key, value})。

7. 执行结果说明

运行上面的示例代码,你会得到如下输出:

Inserted student: (1, Alice)
Inserted student: (2, Bob)
Insertion failed. Key 1 already exists with value: Alice
Inserted student: (3, David)
Final map contents:
1 => Alice
2 => Bob
3 => David

结果解释:

  1. 前两次插入(键1和键2)都成功了,所以打印了插入的信息。
  2. 第三次尝试插入键1(值为"Charlie")时失败了,因为键1已存在(其值为"Alice")。程序打印出了失败信息和已存在的值。
  3. 第四次插入(键3)使用 C++17 语法,成功插入。
  4. 最后遍历整个 map,可以看到只有三个元素,重复插入的 “Charlie” 并没有出现,证明了 insert 的保护性。

8. 图文总结 (Mermaid流程图)

下面这个流程图总结了 std::map::insert 函数的执行逻辑和返回值处理过程:

flowchart TD
A["Start insert(std::pair<const Key, T> value)"] --> B{"Does the key\nalready exist in the map?"}
B -- Yes (Key exists) --> C[Insertion fails]
C --> D["Return a pair:
iterator (points to existing element)
bool (false)"]
D --> E["End (No change to map)"]
B -- No (Key is new) --> F[Insertion succeeds]
F --> G["Return a pair:
iterator (points to new element)
bool (true)"]
G --> H["End (New element added)"]

流程图解读:
该流程图清晰地展示了 insert 函数的决策过程:

  1. 函数开始后,首先检查待插入的键(Key)是否在 map 中已存在。
  2. 如果存在:插入失败,函数返回一个 pair,其中迭代器指向已存在的元素,bool 值为 falsemap 内容不发生任何变化。
  3. 如果不存在:插入成功,新键值对被添加到 map 中,函数返回一个 pair,其中迭代器指向新插入的元素,bool 值为 true

这个“检查-决策-返回”的过程完美地体现了 insert 函数安全、不覆盖的特性。

到此这篇关于C++ 中 `std::map` 的 `insert` 函数的文章就介绍到这了,更多相关C++ std::map insert函数内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文