C/C++ -容器map
目录
容器特性
容器特性
构造对象
默认构造函数
列表构造函数
对象数据
函数:at
大小查询
函数:size()
函数:empty()
增加删除
函数:operator[]
函数:insert()
函数:erase()
函数:clear()
容器特性
-
C++中的map是一个关联容器,它存储键值对元素,其中键唯一。提供快速的基于键的查找,它的元素是根据键排序的。
-
容器特性
- 自动排序:元素按照键的顺序存储,通常是使用键的比较函数排序。
- 键的唯一性:每个键只映射到一个值。
- 访问效率:对元素的查找、插入和删除操作提供对数时间复杂度。
- 透明内存管理:自动内部内存管理,动态分配和回收内存。 使用场景:
构造对象
-
map中的所有元素都是pair对组。
-
pair中的元素一为key(排序),元素二为value。
-
map容器不允许重复元素,mutilmap容器允许重复元素。
-
默认构造函数
- 用途:创建一个空的 map 对象。
- 语法:std::map<int, std::string> map1;
-
列表构造函数
- 用途:使用初始化列表构造 map,允许直接列表初始化。
- 语法:std::map<int, std::string> init_list_map{{1, "one"}, {2, "two"}, {3, "three"}};
对象数据
-
函数:at
-
用途:访问键对应的值。
-
语法:mapped_type& at(const key_type& key);
-
返回值:返回键对应的值的引用。(如果键不存在,会抛出std::out_of_range异常。)
#include <iostream> #include <map> int main() { std::map<int, std::string> myMap { {1, "anlian"}, {2, "zhuge"}, {3, "mengxin"} }; //iter for (auto i = myMap.begin(); i != myMap.end(); i++) { std::cout << (*i).first << std::endl; std::cout << (*i).second << std::endl; std::cout << i->first << std::endl; std::cout << i->second << std::endl; } //at std::cout << myMap.at(2) << std::endl; return 0; }
-
大小查询
-
函数:size()
-
用途:用来获取map容器中元素的数目。
-
语法:size_type size() const noexcept;
-
返回值:返回容器中元素的个数(类型为size_type,通常是size_t类型)。
#include <iostream> #include <map> int main() { std::map<int, std::string> myMap; // 向map中插入一些键值对 myMap.insert(std::make_pair(1, "One")); myMap.insert(std::make_pair(2, "Two")); myMap.insert(std::make_pair(3, "Three")); // 使用size()函数获取map的大小 std::cout << "The size of the map is: " << myMap.size() << std::endl; return 0; }
-
-
函数:empty()
-
用途:检查容器是否为空。
-
语法:bool empty() const noexcept;
-
返回值:如果map为空,返回true;否则返回false。
#include <iostream> #include <map> int main() { std::map<int, std::string> myMap; // 检查map是否为空 std::cout << "Is map empty? " << (myMap.empty() ? "Yes" : "No") << std::endl; // 输出: Is map empty? Yes myMap.insert(std::pair<int, std::string>(1, "Apple")); // 再次检查map是否为空 std::cout << "Is map empty? " << (myMap.empty() ? "Yes" : "No") << std::endl; // 输出: Is map empty? No return 0; }
-
增加删除
-
函数:operator[]
-
用途:访问键对应的值。
-
语法:mapped_type& operator[](const key_type& key);
-
返回值:返回键对应的值的引用。
#include <iostream> #include <map> int main() { std::map<int, std::string> myMap; //[] myMap[1] = "anlian"; myMap[2] = "zhuge"; myMap[3] = "mengxin"; std::cout << myMap[1] << std::endl; std::cout << myMap[2] << std::endl; std::cout << myMap[3] << std::endl; return 0; }
-
-
函数:insert()
-
用途:在map中添加键值对。
-
语法:pair
-
返回值:返回一个pair对象,包含一个迭代器和一个bool值。迭代器指向已插入元素或阻止插入的元素,bool值表示插入是否成功。
#include <iostream> #include <map> int main() { std::map<int, std::string> myMap; auto retpair = myMap.insert(std::pair<int, std::string>(1, "anlian")); if (retpair.second) { std::cout << "success" << std::endl; } return 0; }
-
-
函数:erase()
-
用途:从map中删除元素。
-
语法:
// 通过key删除 size_type erase(const key_type& k); // 通过迭代器删除 iterator erase(const_iterator position); // 通过迭代器范围删除 iterator erase(const_iterator first, const_iterator last);
-
返回值:对于第一个语法形式,返回被删除元素的数量。对于其它两个语法形式,返回void。
#include <iostream> #include <map> int main() { std::map<int, std::string> myMap; // 插入元素 myMap.insert(std::pair<int, std::string>(1, "Apple")); // 通过key删除 auto num = myMap.erase(1); std::cout << num << " elements removed." << std::endl; return 0; }
-
-
函数:clear()
-
用途:访问键对应的值。
-
语法:void clear() noexcept;
-
返回值:清除map中的所有元素。
#include <iostream> #include <map> int main() { std::map<int, std::string> myMap; myMap.insert(std::pair<int, std::string>(1, "Apple")); myMap.insert(std::pair<int, std::string>(2, "Banana")); // 删除所有元素 myMap.clear(); std::cout << "Map size: " << myMap.size() << std::endl; // 输出: Map size: 0 return 0; }
-