欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > C++中的map容器详解

C++中的map容器详解

2024/10/23 23:24:42 来源:https://blog.csdn.net/weixin_47987343/article/details/139581322  浏览:    关键词:C++中的map容器详解

C++中的map容器是一种关联式容器,提供了键-值对(key-value pair)的存储和快速查找功能。map容器由标准模板库(STL)提供,包含在<map>头文件中。map使用平衡二叉树(通常是红黑树)实现,因此能够在对数时间复杂度内完成插入、删除和查找操作。

基本特点

  • 键值唯一性:每个键(key)在map中是唯一的,不能重复。
  • 自动排序map根据键的大小自动排序,默认使用小于运算符(<)比较键的大小,可以通过指定自定义比较器改变排序规则。
  • 键和值的类型:键和值可以是任何可比较和可复制的类型。
  • 双向迭代器map提供双向迭代器,可以从头到尾或从尾到头遍历元素。

常用操作

创建和初始化
#include <iostream>
#include <map>
#include <string>int main() {// 创建一个空的 mapstd::map<int, std::string> myMap;// 使用初始化列表初始化 mapstd::map<int, std::string> initMap = {{1, "One"},{2, "Two"},{3, "Three"}};return 0;
}

插入元素

int main() {std::map<int, std::string> myMap;// 使用 insert() 方法插入元素myMap.insert(std::make_pair(1, "One"));myMap.insert(std::pair<int, std::string>(2, "Two"));// 使用下标操作符插入元素myMap[3] = "Three";// 插入重复键的元素(不会插入)auto result = myMap.insert(std::make_pair(1, "One Again"));if (!result.second) {std::cout << "Key 1 already exists with value: " << result.first->second << std::endl;}return 0;
}

访问元素

int main() {std::map<int, std::string> myMap = {{1, "One"},{2, "Two"},{3, "Three"}};// 使用下标操作符访问元素std::cout << "Key 1 has value: " << myMap[1] << std::endl;// 使用 at() 方法访问元素try {std::cout << "Key 2 has value: " << myMap.at(2) << std::endl;} catch (const std::out_of_range& e) {std::cout << e.what() << std::endl;}// 使用 find() 方法查找元素auto it = myMap.find(3);if (it != myMap.end()) {std::cout << "Key 3 has value: " << it->second << std::endl;}return 0;
}

删除元素

int main() {std::map<int, std::string> myMap = {{1, "One"},{2, "Two"},{3, "Three"}};// 使用 erase() 方法删除指定键的元素myMap.erase(2);// 使用迭代器删除元素auto it = myMap.find(3);if (it != myMap.end()) {myMap.erase(it);}return 0;
}

遍历元素

int main() {std::map<int, std::string> myMap = {{1, "One"},{2, "Two"},{3, "Three"}};// 使用范围for循环遍历元素for (const auto& pair : myMap) {std::cout << "Key: " << pair.first << ", Value: " << pair.second << std::endl;}// 使用迭代器遍历元素for (auto it = myMap.begin(); it != myMap.end(); ++it) {std::cout << "Key: " << it->first << ", Value: " << it->second << std::endl;}return 0;
}

常用成员函数

  • 构造函数

    • 默认构造函数:map()
    • 拷贝构造函数:map(const map& other)
    • 移动构造函数:map(map&& other)
  • 元素访问

    • at(const key_type& k): 返回键为k的元素的引用,如果键不存在则抛出异常。
    • operator[](const key_type& k): 返回键为k的元素的引用,如果键不存在则插入一个默认值。
  • 迭代器

    • begin(), end(): 返回指向容器首尾的迭代器。
    • rbegin(), rend(): 返回指向容器逆向首尾的迭代器。
  • 容量

    • empty(): 检查容器是否为空。
    • size(): 返回容器中元素的数量。
    • max_size(): 返回容器最大可能包含的元素数量。
  • 修改

    • insert(): 插入元素。
    • erase(): 删除指定键或迭代器位置的元素。
    • clear(): 清空容器。
  • 查找

    • find(const key_type& k): 查找键为k的元素,返回指向该元素的迭代器,如果不存在则返回end()
    • count(const key_type& k): 返回键为k的元素个数(对于map而言,要么是0要么是1)。
  • 比较

    • key_comp(), value_comp(): 返回用于比较键和值的比较器对象。

通过了解和掌握这些基本操作和成员函数,可以高效地使用map容器来管理和操作键值对数据。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com