map容器(重要)

11 篇文章 2 订阅

1、map容器简介

Map容器中所有的元素都会根据元素的键值自动实现排序。Map中所有的元素都是pair,pair的简介
同时拥有实值和键值,而前面的 set 只是拥有 键值,pair 中的 第一个元素是键值,而第二个值是 实值。Map 中是不允许有两个相同的键值的,就像一个学校中一样,学生的学号就是键值,而学号对应的学生的具体信息就是实值。而且,我们不能通过Map 的迭代器改变map的键值,因为map 的键值关系到 map 元素的排列规则,任意改变map 的键值将会严重破坏 map 的组织。但是修改键值所对应的实值是可以的,Map 和 list 拥有相同的某些性质,当对它容器中的元素进行插入或者删除的时候,操作之前的所有迭代器,在操作完成之后依然是有效的,当然那个被删除元素的迭代器必然是一个例外。Multilmap 和 map 的操作类似,唯一的区别是 multimap 的键值是可以重复的,Map 和 multimap 的底层都是以 红黑树为实现机制的
Map的键值是不能修改的,因为它是根据键值来实现排序的

2、Map 容器 API 操作

建立 Person 类

#include <iostream>
#include <map>
using namespace std;
class Person
{
    friend void test01();
    friend void printMapAll(map<int, Person> &m);
private:
    int num;
    string name;
    float score;
public:
    Person(){}
    Person(int num,string name, float score);
};

Person::Person(int num, string name, float score)
{
    this->num = num;
    this->name = name;
    this->score = score;
}

打印学生信息

void printMapAll(map<int, Person> &m)
{
    map<int, Person>::const_iterator it;
    for(it=m.begin(); it!=m.end();it++)
    {
        //(*it) ==pair<int, Person>
        cout<<"学号:"<<(*it).first<<" 姓名:"<<(*it).second.name<<" \
分数:"<<(*it).second.score<<endl;
    }
}

test01函数实现 Map 对应的 API 操作

void test01()
{
    //Map和set一样唯一的插入方式,不能同时插入 n 个一样的构造函数,因为键值不能重复,所以只能用这种方式插入 
    map<int, Person> m;
    //方式1:
    m.insert(pair<int,Person>(103, Person(103,"lucy", 88.8f)));
    //方式2:推荐
    m.insert(make_pair(101,Person(101,"bob", 77.7f)));
    //方式3:
    m.insert( map<int, Person>::value_type( 102 , Person(102,"tom", 66.6f)));
    //方式4:
    m[104] = Person(104,"德玛", 99.9f);

    printMapAll(m);

    //假如key值存在  m[key]代表对应的实值,但是最好不要这么访问操作,因为要是 键值 107 不存在,就会添加一个学生信心,所以不推荐使用这种方式访问 
    cout<< m[107].num<<" "<<m[107].name<<" "<<m[107].score<<endl;

    cout<<"----------使用m[107].num产生问题------------"<<endl;
    printMapAll(m);

    m.erase(104);       //可以删除迭代器,也可以删除元素 
    cout<<"----------"<<endl;
    printMapAll(m);

    //查找key为103的数据
    map<int, Person>::const_iterator ret;
    ret = m.find(103);      //find的返回值是当前容器Map的迭代器 
    if(ret != m.end())
    {
        //*ret == pair<int,Person>
        cout<<(*ret).first<<" "<<(*ret).second.name<<" "<<(*ret).second.score<<endl;
    }
}

主函数调用 test01


int main(int argc, char *argv[])
{
    test01();
    return 0;
}

——————————————————————————————————————————————
所有代码如下
#include
#include
using namespace std;
class Person
{
friend void test01();
friend void printMapAll(map<int, Person> &m);
private:
int num;
string name;
float score;
public:
Person(){}
Person(int num,string name, float score);
};

Person::Person(int num, string name, float score)
{
this->num = num;
this->name = name;
this->score = score;
}

void printMapAll(map<int, Person> &m)
{
map<int, Person>::const_iterator it;
for(it=m.begin(); it!=m.end();it++)
{
//(*it) ==pair<int, Person>
cout<<“学号:”<<(*it).first<<" 姓名:“<<(*it).second.name<<”
分数:"<<(*it).second.score<<endl;
}
}

void test01()
{
//Map和set一样唯一的插入方式,不能同时插入 n 个一样的构造函数,因为键值不能重复,所以只能用这种方式插入
map<int, Person> m;
//方式1:
m.insert(pair<int,Person>(103, Person(103,“lucy”, 88.8f)));
//方式2:推荐
m.insert(make_pair(101,Person(101,“bob”, 77.7f)));
//方式3:
m.insert( map<int, Person>::value_type( 102 , Person(102,“tom”, 66.6f)));
//方式4:
m[104] = Person(104,“德玛”, 99.9f);

printMapAll(m);

//假如key值存在 m[key]代表对应的实值,但是最好不要这么访问操作,因为要是 键值 107 不存在,就会添加一个学生信心,所以不推荐使用这种方式访问
cout<< m[107].num<<" “<<m[107].name<<” "<<m[107].score<<endl;

cout<<“----------使用m[107].num产生问题------------”<<endl;
printMapAll(m);

m.erase(104); //可以删除迭代器,也可以删除元素
cout<<“----------”<<endl;
printMapAll(m);

//查找key为103的数据
map<int, Person>::const_iterator ret;
ret = m.find(103); //find的返回值是当前容器Map的迭代器
if(ret != m.end())
{
//*ret == pair<int,Person>
cout<<(*ret).first<<" “<<(*ret).second.name<<” "<<(*ret).second.score<<endl;
}
}

int main(int argc, char *argv[])
{
test01();
return 0;
}
——————————————————————————————————————————————
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Q渡劫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值