Map常用操作——插入、查找、删除、遍历

本文详细介绍了C++ STL中Map容器的四种插入方法、三种查找方式、四种删除操作及遍历技巧。深入探讨了每种操作的特点及应用场景,帮助读者全面掌握Map的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Map常用操作——插入、查找、删除、遍历

1. 插入

四种插入方法
方法一:pair

map<int, int> mp;
mp.insert(pair<int,int>(1, 2));

方法二:make_pair

map<int, int> mp;
mp.insert(make_pair<int,int>(2,3));

方法三:value_type

map<int, int> mp;
mp.insert(map<int, int>::value_type(3,4));

方法四:[]

map<int, int> mp;
mp[4] = 5;

四种方法异同:
前三种方法当出现重复键时,编译器会报错,有的可能不会报错但是会忽视重复键插入,而第四种方法,当键重复时,会覆盖掉之前的键值对。

2. 查找

方法一:[]

map<int, int> mp;
cout << mp[1] << endl;
Mp[1] = 2;
cout << mp[1] << endl;

第一句输出为0,第二句输出为2.
下标索引的机制:用下标访问map中不存在的元素时,将导致向map中添加该下标所指向的新元素,其值会根据类型默认值或默认构造函数进行初始化。
方法二:map.at()

map<int, int> mp;
map[1] = 0;
cout << mp.at(1) << endl;
cout << mp.at(2) << endl;

第一句输出为0,第二句抛出异常。.
at函数机制:存在则正常输出,不存在返回map.end().
方法三:count()和find()

map<int,int> mp;
    mp[1] = 1;
     map<int, int>::iterator iter=mp.find(1);
     if(iter != mp.end()){
          cout<<"Found, the value is "<<iter->second<<endl;
     }else{
          cout<<"Do not found"<<endl;
    }

map::find(k),如果map中存在按k索引的元素,则返回指向该元素的iterator;如果不存在则返回end()。
map::count(k),返回map中k出现的次数,为0当然就表示不存在,只能用来判断k是否存在。

3. 删除

map定义了四个删除元素的方法

iterator erase(iterator it); //通过一个条目对象删除
iterator erase(iterator first, iterator last);//删除范围[first, last)内对象
size_type erase(const Key& key); //通过关键字删除
map的清空函数clear()就相当于 Map.erase(Map.begin(), Map.end());

4. 遍历

前序遍历 [ begin(), end() )

map<int, int> mp;
for (map<int, int>::iterator iter = mp.begin(); iter != mp.end(); iter ++)
    cout << mp->first << " " << mp->second << endl;

后序遍历 [ rbegin(), rend() )

map<int,string> mp;
for (map<int, int>::iterator iter = mp.rbegin(); iter != mp.rend(); iter ++)
    cout << mp->first << " " << mp->second << endl;
### 遍历 C++ 中的 `std::map` 容器 在 C++ 中,可以通过多种方式来遍历 `std::map` 容器。以下是几种常见的方法及其代码示例。 #### 使用标准迭代器遍历 通过定义一个正向迭代器并将其初始化为 `myMap.begin()` 来开始遍历,直到到达 `myMap.end()` 停止。这是最常见的遍历方式之一[^2]: ```cpp #include <iostream> #include <map> int main() { std::map<int, std::string> myMap = {{1, "one"}, {2, "two"}, {3, "three"}}; for (auto it = myMap.begin(); it != myMap.end(); ++it) { std::cout << "Key: " << it->first << ", Value: " << it->second << std::endl; } return 0; } ``` #### 使用范围基循环 (`range-based for loop`) C++11 引入了更简洁的方式——范围基循环,可以直接访问容器中的每一个键值对: ```cpp #include <iostream> #include <map> int main() { std::map<int, std::string> myMap = {{1, "one"}, {2, "two"}, {3, "three"}}; for (const auto& pair : myMap) { std::cout << "Key: " << pair.first << ", Value: " << pair.second << std::endl; } return 0; } ``` #### 反向遍历 `std::map` 如果希望从最后一个元素到第一个元素进行逆序遍历,则可以使用反向迭代器 `rbegin()` 和 `rend()`: ```cpp #include <iostream> #include <map> int main() { std::map<int, std::string> myMap = {{1, "one"}, {2, "two"}, {3, "three"}}; for (auto rit = myMap.rbegin(); rit != myMap.rend(); ++rit) { std::cout << "Key: " << rit->first << ", Value: " << rit->second << std::endl; } return 0; } ``` 以上三种方法都可以用来遍历 `std::map` 容器,具体选择取决于实际需求和个人偏好。 ### 性能对比与适用场景 相对于 `std::unordered_map` 而言,虽然 `std::map` 提供有序存储的优势,但在插入查找操作上其平均性能通常较低,并且可能带来更高的内存开销[^3]。因此,在不需要保持顺序的情况下可以选择 `std::unordered_map` 进行优化。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值