map学习之删除操作clear,erase,swap,extract,merge

本篇学习map的删除操作,示例代码如下:

#include <map>
#include <string>

using namespace std;

void deleteOperator()
{
    //1.clear清除内容
    map<int, string> map1;
    map1[1] = "hello1";
    map1[2] = "hello2";
    map1[3] = "hello3";
    cout << "1 size = " << map1.size() << " empty = " << map1.empty() << endl;
    map1.clear();
    cout << "2 size = " << map1.size() << " empty = " << map1.empty() << endl;
    //2.erase擦除元素
    map1.insert({11, "hello11"});
    map1.emplace(12, "hello12");
    map1.insert({13, "hello13"});
    map1.insert({14, "hello14"});
    auto mapIter = map1.begin();
    while(mapIter != map1.end())
    {
        cout << mapIter->first << ": " << mapIter->second << endl;
        ++mapIter;
    }
    map1.erase(13);//参数key
    cout << endl;
    auto mapIter2 = map1.begin();
    while(mapIter2 != map1.end())
    {
        cout << mapIter2->first << ": " << mapIter2->second << endl;
        ++mapIter2;
    }
    cout << endl;
    //3.swap交换内容
    map<int, string> map2;
    map2[21] = "camel21";
    map2[22] = "camel22";
    map2[23] = "camel23";
    auto map2Iter = map2.begin();
    while(map2Iter != map2.end())
    {
        cout << map2Iter->first << ": " << map2Iter->second << endl;
        ++map2Iter;
    }
    map2.swap(map1);
    cout << "map1: " << endl;
    for(auto map1iter = map1.begin(); map1iter != map1.end(); ++map1iter)
    {
        cout << map1iter->first << ": " << map1iter->second << endl;
    }
    cout << endl;
    cout << "map2: " << endl;
    for(auto map2iter = map2.begin(); map2iter != map2.end(); ++map2iter)
    {
        cout << map2iter->first << ": " << map2iter->second << endl;
    }
    cout << endl;
    //4.extract从另一容器释出结点   C++17 提取
    cout << "map3: " << endl;
    map<int, string> map3{{1,"apple"}, {5,"pear"}, {3,"peach"}, {4,"grape"}};
    auto nh = map3.extract(5);//参数key
    nh.key() = 2;
    map3.insert(move(nh));
    for(auto map3iter = map3.begin(); map3iter != map3.end(); ++map3iter)
    {
        cout << map3iter->first << ": " << map3iter->second << endl;
    }
    cout << "map3.size = " << map3.size() << endl;
    //5.merge从另一容器接合结点   C++17
    map<int, string> map4{{2, "durian"}, {5, "mango"}, {10, "guava"}};;
    map<int, string> map5{{1, "strawberry"}, {4, "melon"}, {5, "banana"}, {8, "orange"}};
    map5.merge(map4);//如果有相同的关键字不会合并,例如5
    cout << "map4: " << endl;
    for(auto const &value: map4)
    {
        cout << value.first << ": " << value.second << endl;
    }
    cout << endl;
    cout << "map5: " << endl;
    for(auto const &value: map5)
    {
        cout << value.first << ": " << value.second << endl;
    }
    cout << endl;
}

int main()
{
    deleteOperator();
    cout << "Hello, world!" << endl;
    return 0;
}

运行结果如下:

参考:

http://www.cplusplus.com/reference/map/map/
https://zh.cppreference.com/w/cpp/container/map

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值