C++ Map容器的使用和概念

本文详细介绍了C++中的map容器,包括其底层实现(哈希的红黑树)、基本操作如插入、删除、查找、统计等,并展示了如何自定义排序规则。此外,还通过实例演示了map与multimap的区别以及如何进行大小交换。文章旨在帮助读者深入理解并熟练运用map容器。
摘要由CSDN通过智能技术生成
#include"iostream"
#include"map"
#include"algorithm"
#include"string"


using namespace std ;

// 什么是map容器?
// map中所有的元素都是pair
// 什么是pair?
// pair是一个成对出现的数据。
// 如下程序:

void pairs()
{
    // 创建一个数对
    pair<string,double> p1(string("clucy"),120);

    // 输出数对
    cout<<p1.first<<p1.second<<endl;
//  修改pair
    p1.first="Blocla";
    p1.second=150;

    cout<<p1.first<<p1.second<<endl;

}

// 但是在map中,pair的第一个值为键值,第二个值为实值。

// 所有的元素都会根据元素的键值自动排序。

// 本质:
// 底层是由带哈希的红黑树实现。

// 优点:
// 可以根据key值快速的找到value的值。

// map和multimap的区别
// map只能允许一个value值,而mutimap相反。

// 生成数据到map
void genMap(multimap<string,double> &mp)
{
    for(int i=0;i<10;i++)
    {
        mp.insert(make_pair("a",i+1));
    }
}
// 打印map
void printMap(multimap<string,double> &mp)
{
    for(multimap<string,double>::iterator it = mp.begin();it!=mp.end();it++)
    {
        cout<<(*it).first<<(*it).second<<endl;
    }
}
// map的构造
void constructor()
{
    // 默认构造
    multimap<string,double> mp;
    // 生成数据
    genMap(mp);
    // 打印数据
    printMap(mp);

    // 拷贝构造

    multimap<string,double> mp2(mp);

    printMap(mp2);

    // 赋值
    multimap<string,double> mp3= mp;

    printMap(mp3);




}

// 大小交换
void mapSizeAndSwapMap()
{
    multimap<string,double> mp;
    genMap(mp);
    printMap(mp);

    cout<<"The size is ::"<<mp.size()<<endl;
    // 判断map是否为空
    if(mp.empty())
    {
        cout<<"This map is not empty"<<endl;
    }
    else 
    {
        cout<<"This map is empty"<<endl;
    }

    // 交换两个map的值
    multimap<string,double> mp2;
    mp2.insert(make_pair("b1,",23));
    mp2.insert(make_pair("b2,",42));
    mp2.insert(make_pair("b3,",34));

// 
    mp.swap(mp2);

    printMap(mp);
    printMap(mp2);


}

// map的插入和删除
void mapInsertAndDelete()
{
    multimap<string,double> mp;
    // 插入元素
    mp.insert(make_pair("Zanpian",234.2));
    mp.insert(make_pair("Zanpian",234.2));
    mp.insert(make_pair("Zanpian",234.2));
    printMap(mp);



    // 迭代器的方式删除
    mp.erase(mp.begin());

    // 范围方式删除
     mp.erase(mp.begin(),mp.end());


    // 删除key值为:Zanpian,这种方法只能安装key值的方式删除元素
    mp.erase("Zanpian");
    printMap(mp); 

    // 清除map
    mp.clear();
    printMap(mp);

}
// map的查找和统计
void mapFindAndCount()
{
    multimap<string,double> mp;
    genMap(mp);
    printMap(mp);

//  查找map的key值为a的元素
    multimap<string,double>::iterator it = mp.find("a");
// 输出
    cout<<"the result is ::";
    cout<<it->first<<it->second<<endl;

// 统计key为a的个数
    cout<<mp.count("a")<<endl;
    
}


// 自定义数据类型的排序规则仿函数
class Cat{
    string name;
    public:
    int age;
public:
    Cat(string name,int age)
    {
        this->name = name;
        this->age = age;
    }

    void showInfo()
    {
        cout<<this->name<<this->age<<endl;
    }

};
// 定义排序规则
class mapSort{
    public:
    bool operator()(Cat &c1,Cat &c2)
    {
        return c1.age>c2.age;
    }
};
void mapClassSort()
{
    multimap<int,Cat> mp;
    mp.insert(make_pair(1,Cat("Nany",23)));
     mp.insert(make_pair(1,Cat("Nany",2)));
      mp.insert(make_pair(1,Cat("Nany",1)));
       mp.insert(make_pair(1,Cat("Nany",3)));



    // cout<<mp.begin()->first;
    // mp.begin()->second.showInfo();

    // 打印输出
     for(multimap<int,Cat>::iterator it = mp.begin();it!=mp.end();it++)
     {
        cout<<(*it).first<<endl;
        (*it).second.showInfo();
     }

}



int main()
{


    mapClassSort();
    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值