map/multimap用法

map与multimap为关联容器,结构如下

map底层实现依然是rb_tree 他的data可以改,但是key不能改,因此map仍然具有自动排序的功能

我们无法使用迭代器改变元素的key(const key),但是可以改变元素的data.

map的key必须独一无二,multimap的key可以重复

map的定义函数

复制代码
  template <typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
            typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
    class map
    {
    public:
      typedef _Key                                          key_type;
      typedef _Tp                                           mapped_type;
      typedef std::pair<const _Key, _Tp>                    value_type;
      typedef _Compare                                      key_compare;
      typedef _Alloc                                        allocator_type;
      ...
}
复制代码

参数1 class key 键值key

参数2 class T data

参数3 class compare 排序key的函数 默认为less() 升序

参数4 alloc 分配器

 


 

 

map的基本使用

一 定义

复制代码
    //构造函数
    map<int, int> c;
    c[1] = 10;
    c[2] = 20;
    c[3] = 30;
    c[4] = 40;
    c[5] = 50;
    for(auto i : c)
    {
        cout<<"["<< i.first <<"] = " << i.second <<"  ";
    }
    cout << endl;
    
    //operator =
    map<int, int> c1;
    c1 = c;
    for(auto i : c)
    {
        cout<<"["<< i.first <<"] = " << i.second <<"  ";
    }
    cout << endl;
复制代码

 


 

二 迭代器操作 map的迭代器就是红黑树的迭代器

复制代码
    //迭代器操作
    /*
    map<int, int> c;
    c.insert({pair<int, int>(1,10),pair<int, int>(2,20),pair<int, int>(3,30),pair<int, int>(4,40),pair<int, int>(5,50),pair<int, int>(6,60)});
    */

    //begin()
    map<int, int>::iterator iter;
    iter = c.begin();
    cout<< "begin(): "<<"["<< iter->first <<"] = " << iter->second <<endl;
    
    //end()
    iter = c.end();
    iter--;
    cout<<"end(): " <<"["<< iter->first <<"] = " << iter->second <<endl;
    
    //rbegin()反向头迭代器
    map<int, int>::reverse_iterator riter;
    riter = c.rbegin();
    cout << "rbegin(): "<<"["<< riter->first <<"] = " << riter->second <<endl;
    
    //rend()反向头迭代器
    riter = c.rend();
    riter--;
    cout << "rend(): "<<"["<< riter->first <<"] = " << riter->second <<endl;
    
    //cbegin() const 迭代器 正向 头迭代器
    map<int, int>::const_iterator citer;
    citer = c.cbegin();
    cout << "cbegin(): "<<"["<< citer->first <<"] = " << citer->second <<endl;
    
    //cend() const 迭代器 反向 尾迭代器
    citer = c.cend();
    citer--;
    cout<< "cend(): "<<"["<< citer->first <<"] = " << citer->second <<endl;
复制代码

 

 

 


 

三 容量

复制代码
    //容量
    /*
     map<int, int> c;
     c.insert({pair<int, int>(1,10),pair<int, int>(2,20),pair<int, int>(3,30),pair<int, int>(4,40),pair<int, int>(5,50),pair<int, int>(6,60)});
     */
    //是否为kong
    cout << "empty: " << c.empty() <<endl;
    
    //元素个数
    cout << "size: " << c.size() <<endl;
    
    //最大容量
    cout << "max_size: " << c.max_size() <<endl;
复制代码

 


 

四 基本操作

复制代码
//基本操作
    /*
     map<int, int> c;
     c.insert({pair<int, int>(1,10),pair<int, int>(2,20),pair<int, int>(3,30),pair<int, int>(4,40),pair<int, int>(5,50),pair<int, int>(6,60)});
     */
    
    //operator[] 这个和其他容器的operator有些不同,如果[index] 的index(key) 在map中存在则直接返回该key对应的data ,如果不存在则想该位置插入默认值
    cout << "operator[]: " << c[1] << endl;
    cout << "operator[]: " << c[10] << endl; //这时你会发现 map自动插入一个c[10]
    for(auto i : c)
    {
        cout<<"["<< i.first <<"] = " << i.second <<"  ";
    }
    cout << endl;
    
    //at() 取data
    cout<< "at(): " << c.at(3) << endl;
    
    //插入insert() map 不允许key重复 插入重复key 不会报错但插入不成功
    c.insert(pair<int, int>(6, 15));
    cout <<"insetr(): ";
    for(auto i : c)
    {
        cout<<"["<< i.first <<"] = " << i.second <<"  ";
    }
    cout << endl;
    

    map<int, int> c_insert;
    map<int, int>::iterator insert_iter = c_insert.begin();
    c_insert.insert(insert_iter,pair<int, int>(100, 10000));
    cout <<"insetr(): ";
    for(auto i : c_insert)
    {
        cout<<"["<< i.first <<"] = " << i.second <<"  ";
    }
    cout << endl;
    
    
    //删除
    map<int,int>::iterator erase_iter = c.begin();
    c.erase(erase_iter);
    cout <<"erase(): ";
    for(auto i : c)
    {
        cout<<"["<< i.first <<"] = " << i.second <<"  ";
    }
    cout << endl;
    //指定下表删除
    c.erase(10);
    cout <<"erase(): ";
    for(auto i : c)
    {
        cout<<"["<< i.first <<"] = " << i.second <<"  ";
    }
    cout << endl;
    
    //指定元素删除
    erase_iter = c.find(6);
    if(erase_iter != c.end())
    {
        cout<<"found index: "<< erase_iter->first <<endl;
        c.erase(erase_iter);
    }
    else{
        cout<< "Not found" <<endl;
    }
    for(auto i : c)
    {
        cout<<"["<< i.first <<"] = " << i.second <<"  ";
    }
    cout << endl;
    
    
    //交换swap
    map<int, int> c_swap1;
    c_swap1.insert({pair<int, int>(1,10),pair<int, int>(2,20),pair<int, int>(3,30),pair<int, int>(4,40),pair<int, int>(5,50),pair<int, int>(6,60)});
    map<int, int> c_swap2;
    cout<<"swap() before: "<<endl;
    cout<<"c_swap1: ";
    for(auto i : c_swap1)
    {
        cout<<"["<< i.first <<"] = " << i.second <<"  ";
    }
    cout << endl;
    
    cout<<"c_swap2: ";
    for(auto i : c_swap2)
    {
        cout<<"["<< i.first <<"] = " << i.second <<"  ";
    }
    cout << endl;
    
    cout<<"swap() after: ";
    c_swap2.swap(c_swap1);
    cout<<"c_swap1: ";
    for(auto i : c_swap1)
    {
        cout<<"["<< i.first <<"] = " << i.second <<"  ";
    }
    cout << endl;
    
    cout<<"c_swap2: ";
    for(auto i : c_swap2)
    {
        cout<<"["<< i.first <<"] = " << i.second <<"  ";
    }
    cout << endl;
    
    //清除clear
    c_insert.clear();
    cout <<"clear(): ";
    for(auto i : c_insert)
    {
        cout<<"["<< i.first <<"] = " << i.second <<"  ";
    }
    cout << endl;
    
    //elements() 插入 如果存在什么也不做 如果不存在插入
    map<int, int>::iterator element_iter = c_swap2.begin();
    
    auto xxx = c_swap2.emplace(pair<int, int>(7,60));
    if(xxx.second)
    {
        cout << "不存在" <<endl;
    }
    else
    {
        cout<< "存在" <<endl;
    }
    cout <<"elements(): ";
    for(auto i : c_swap2)
    {
        cout<<"["<< i.first <<"] = " << i.second <<"  ";
    }
    cout << endl;
    
    //emplace_hint() 插入
    element_iter = c.emplace_hint(element_iter, pair<int, int>(7,60));
    cout <<"emplace_hint(): ";
    if(element_iter != c_swap2.end())
    {
        cout<< "存在" <<endl;
    }
    else{
        cout << "不存在" <<endl;
    }
复制代码

 


 

五 操作函数

复制代码
    //函数
    /*
     map<int, int> c;
     c.insert({pair<int, int>(1,10),pair<int, int>(2,20),pair<int, int>(3,30),pair<int, int>(4,40),pair<int, int>(5,50),pair<int, int>(6,60)});
     */
    //key_comp 返回key排序的函数 返回仿函数
    cout<<"key_comp(): " << c.key_comp()(1,2) <<endl; //会返回1  因为1<2
    cout<<"key_comp(): " << c.key_comp()(2,1) <<endl; //会返回0  因为2>1
    cout<<"key_comp(): " << c.key_comp()(1,1) <<endl; //会返回0  因为1=1
    
    //value_comp 返回取value和key数据包中的 取key函数 返回仿函数
    pair<int,int> value_comp_pair = *c.begin();
    iter = c.begin();
    cout << c.value_comp()(*iter++,value_comp_pair) << endl;
复制代码

 


 

六 算法

复制代码
//算法
    /*
     map<int, int> c;
     c.insert({pair<int, int>(1,10),pair<int, int>(2,20),pair<int, int>(3,30),pair<int, int>(4,40),pair<int, int>(5,50),pair<int, int>(6,60)});
     */
    //find()  指定key 返回对应pair
    cout <<"find(1): " << c.find(1)->second << endl;;

    
    //count()   key出现的次数
    cout <<"count(1): " << c.count(1)<< endl;;
    
    
    
    c.erase(4);
    
    //lower_bound 返回键值>=给定元素的第一个位置
    auto lower_boundObj = c.lower_bound(8);
    if(lower_boundObj->first)
        cout<<lower_boundObj->first<<endl;
    else
        cout<< "Not found lower_boundObj" << endl;
    
    for(auto i : c)
    {
        cout<<"["<< i.first <<"] = " << i.second <<"  ";
    }
    cout << endl;
    
    //upper_bound 返回键值>给定元素的第一个位置
    auto upper_boundObj = c.upper_bound(4);
    if(upper_boundObj->first)
        cout<<upper_boundObj->first<<endl;
    else
        cout<< "Not found upper_bound" << endl;
    
    for(auto i : c)
    {
        cout<<"["<< i.first <<"] = " << i.second <<"  ";
    }
    cout << endl;
    
    
    
    //equal_range()返回该元素所在区间(闭区间),返回值是一个pair<iterator, iterator>类型,first代表所在区间的起点迭代器,second表示所在区间的终点迭代器
    
    auto equal_rangeObj = c.equal_range(3);
    if(equal_rangeObj.first->first)
    {
        cout<<equal_rangeObj.first->first<<endl;
    }
    else
        cout<< "NOT equal_rangeObj.first" << endl;
    
    if(equal_rangeObj.second->second)
    {
        cout<<equal_rangeObj.second->first<<endl;
    }
    else
        cout<< "NOT second" << endl;
复制代码

 

 


 七 自定义比较函数 map 默认为升序 改为降序

复制代码
class my_compare_
{
public:
    bool operator()(int a, int b)
    {
        return a > b;
    }
};

int main(int argc, char *argv[])
{
    map<int, int, my_compare_> c;
    c.insert({pair<int, int>(1,10),pair<int, int>(2,20),pair<int, int>(3,30),pair<int, int>(4,40),pair<int, int>(5,50),pair<int, int>(6,60)});
    for(auto i : c)
    {
        cout<<"["<< i.first <<"] = " << i.second <<"  ";
    }
    cout << endl;
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【优质项目推荐】 1、项目代码均经过严格本地测试,运行OK,确保功能稳定后才上传平台。可放心下载并立即投入使用,若遇到任何使用问题,随时欢迎私信反馈与沟通,博主会第一时间回复。 2、项目适用于计算机相关专业(如计科、信息安全、数据科学、人工智能、通信、物联网、自动化、电子信息等)的在校学生、专业教师,或企业员工,小白入门等都适用。 3、该项目不仅具有很高的学习借鉴价值,对于初学者来说,也是入门进阶的绝佳选择;当然也可以直接用于 毕设、课设、期末大作业或项目初期立项演示等。 3、开放创新:如果您有一定基础,且热爱探索钻研,可以在此代码基础上二次开发,进行修改、扩展,创造出属于自己的独特应用。 欢迎下载使用优质资源!欢迎借鉴使用,并欢迎学习交流,共同探索编程的无穷魅力! 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip
C++ STL中的mapmultimap是关联容器,用于存储键值对(key-value pairs),其中每个键(key)唯一对应一个值(value)。 map是一个有序容器,根据键的大小进行自动排序,默认按照键的升序进行排序。每个键只能在map中出现一次,如果尝试插入具有相同键的元素,新元素将替代旧元素。 multimap也是一个有序容器,与map不同的是,它允许多个具有相同键的元素存在。多个具有相同键的元素将按照插入的顺序进行存储,而不会自动排序。 这两个容器都提供了一系列的操作函数,如insert、erase、find等,用于插入、删除和查找元素。 以下是一个使用map的简单示例: ```cpp #include <iostream> #include <map> int main() { std::map<std::string, int> scores; scores.insert(std::make_pair("Alice", 90)); scores.insert(std::make_pair("Bob", 80)); scores.insert(std::make_pair("Charlie", 70)); // 查找并输出Bob的分数 std::cout << "Bob's score: " << scores["Bob"] << std::endl; // 遍历并输出所有键值对 for (const auto& pair : scores) { std::cout << pair.first << ": " << pair.second << std::endl; } return 0; } ``` 上述示例中,我们创建了一个存储string类型键和int类型值的map容器scores。通过insert函数依次插入了三个键值对。然后我们通过scores["Bob"]来获取Bob的分数,并输出结果为80。 接着我们使用范围-based for循环遍历map中的所有键值对,并输出每个键值对的键和值。 multimap用法map类似,只是它允许多个具有相同键的元素存在。 这些关联容器在查找和插入操作上具有较高的效率,特别适用于需要根据键进行快速查找的场景。在实际应用中,你可以根据自己的需求选择适合的容器类型。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值