【C++】Map用法

未排版,仅备自查

#include <map>
#include <iostream>
//容量查询
    // // 查询map是否为空
    // bool empty();

    // // 查询map中键值对的数量
    // size_t size();

    // // 查询map所能包含的最大键值对数量,和系统和应用库有关。
    // // 此外,这并不意味着用户一定可以存这么多,很可能还没达到就已经开辟内存失败了
    // size_t max_size();

    // // 查询关键字为key的元素的个数,在map里结果非0即1
    // size_t count( const Key& key ) const; //
//交换
    // 就是两个map的内容互换
    // void swap( map& other );
using namespace std;
//定义map <关键字, 值>
std::map<int, string> hash_map;
int main(){
//一、插入
    //1.1.使用[ ]进行单个插入。重复的插入会顶替旧的值
    hash_map[2015] = "Tom";
    //1.2.使用insert插入(单插和多插)
    //插入单个值
    hash_map.insert(std::pair<int, string>(2016, "Tim"));

    //返回插入位置以及是否插入成功
    pair<std::map<int, string>::iterator, bool> ret;
    ret = hash_map.insert(std::pair<int, string>(2016, "Lara"));
    //ret.second为插入是否成功(bool). 若插入失败,则 ret.first->first, ret.first->second为存在的键-值对
    if (ret.second == false) {
        std::cout << "key already existed";
        std::cout << "Key is: " << ret.first->first << endl;
        std::cout << "Val is: " << ret.first->second << endl;
    }
    else{//若插入成功,则为新的键-值对
        std::cout << "Insertion successful" << endl;
        std::cout << ret.first->first << endl;
        std::cout << ret.first->second << endl;
    }
     //指定位置插入
    std::map<int, string>::iterator it = hash_map.begin();
    hash_map.insert(it, std::pair<int, string>(2017, "Fas"));  //效率更高
    hash_map.insert(it, std::pair<int, string>(2018, "Als"));  //效率非最高
    //范围多值插入
    std::map<int, string> anothermap;
    anothermap.insert(hash_map.begin(), hash_map.find(2017));
    // 列表形式插入
    anothermap.insert({ { 2019, "Niko"}, {2020, "S1mple"}});
//二、通过key访问/修改元素
    //Map中元素取值主要有at和[ ]两种操作,at会作下标检查,而[]不会。
    map<int, string> ID_Name ={{2016, "Gas"}};
    //ID_Name中没有关键字2016,使用[]取值会导致插入
    //因此,下面语句不会报错,但打印结果为空
    cout<<ID_Name[2016].c_str()<<endl;//不建议使用

    //使用at会进行关键字检查,如果存在则返回value,否则抛出out_of _range异常
    //ID_Name.at(2017);//抛出异常
    ID_Name.at(2016) = "Ats";//修改值
    cout << ID_Name.at(2016) << endl;
//三、通过key查找
    //3.1 使用count查找
    int key = 2020;
    if(ID_Name.count(key)>0)//返回值0或1
    {
        cout << "Search Successfully!" << endl;
    }
    else
        cout << "Search Failed!" << endl;
    //3.2 通过迭代器和find查找
    map<int,string>::iterator iter;
    iter = ID_Name.find(key);
    if(iter!=ID_Name.end())
    {   //打印键和值
        cout << iter->first <<endl;
        cout << iter->second << endl;
    }
    else
        cout << "Search Failed!" << endl;
//四、删除
    //4.1 通过key的erase() 可以移除键和参数匹配的元素,然后返回所移除元素的个数,例如:
    std::map<std::string, size_t> people {{ "Fred", 45}, {"Joan", 33},{"Jill", 22}};
    std::string name{"Joan"};
    if(people.erase(name))//返回1,删除成功
        std::cout << name << " was removed." << std::endl;
    else//返回0,不存在该元素,删除失败
        std::cout << name << " was not found" << std::endl;
    //4.2 通过迭代器的erase() 这种情况下,返回的迭代器指向被删除元素的下一个位置.参数不能是结束迭代器。
    //  如果迭代器参数指向的是容器的最后一个元素,删删除后会返回结束迭代器。
    std::map<std::string, size_t>::iterator iter_ = people.erase(people.begin());
    if(iter_ == people.end())
        std::cout << "The last element was removed."<< std::endl;
    else
        std::cout << "The element preceding " << iter->first << "was removed." << std::endl;
    //删除两个迭代器之间的元素(前闭后开),返回的迭代器指向这段元素中最后一个被删除的元素
    auto iter_2 = people.erase(begin(people), end(people));//Erase all
    return 0;
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值