C语言到C++STL菜鸟教程:Map容器

2022-07-1712:58:45编程语言入门到精通Comments1,424 views字数 1609阅读模式

1. 简介文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

Map也是一种关联容器,它是 键—值对的集合,即它的存储都是以一对键和值进行存储的,Map通常也可以理解为关联数组(associative array),就是每一个值都有一个键与之一一对应,因此,map也是不允许重复元素出现的。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

同时map也具备set的相关功能,其底层也会将元素进行自动排序,文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

2. 相关文件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

头文件:#include<map>文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

3.  初始化文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

格式为:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

template class Key, 
           class T,
           class Compare = less<Key>, 
           class Alloc = allocator<pair<const Key,T> > 
           class map;

一共有4个值,其中第一个是键,第二个是值,这两个元素呈现对应的关系,接着第三个元素是比较器,其默认是降序排序,第四个是内存配置器,负责内存的分配和销毁。我们常用的可以直接省去第三和第四个值的输入,只输入键和值即可。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

4.迭代器文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

我们使用map<char,int> s提前建立了一个map文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

C98代码如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

 for(map<char,int>::iterator it=s.begin();it!=s.end();it++){
        cout<< it->first <<" --- " << it->second<<endl;
    }

这里我们需要注意一下,我们不能直接通过*it的输出方式输出值,因为map种含有两个元素,相当于一个struct结构体,是一个复合类型,C/C++中输出复合类型需要我们指定复合类型的值。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

因此我们输出就变成了it->first 指定输出键,it-<second指定输出值(刚好就是英文的第一和第二的意思)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

5.常用接口文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

我们使用map<char,int> s 预先创建了一个map,命名为s,方便举例文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

a)    大小size()文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

返回链表元素的个数文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

函数原型:size_type size() const;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

cout<<s.size()<<endl;   //直接返回栈中元素的个数

b) 插入元素insert()文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

插入一个元素,插入元素的类型必须与创建的容器类型一致文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

函数原型:single element文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

pair<iterator,bool> insert (const value_type& val);文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

template <class P> pair<iterator,bool> insert (P&& val);文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

s.insert(pair<char,int>('d',4));   
//这里的Pair表示一对的关系,相当于struct pair{char a;int b}的一对数据,我们在接下来会详细学pari的用法
//如果觉得这样的方式比较困难,可以试着使用下标的方式进行数据输入
s['d']=4;        //这与上面的效果是一样的。

c) 删除元素erase()文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

删除一个元素,或者是一段区间的元素,将会自动缩减空间使用。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

函数原型:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

iterator erase (iterator position);文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

iterator erase (iterator first, iterator last);文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

使用方法:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

s.erase(s.begin());  //使用迭代器的方法删除第一个元素
s.erase(s.begin(),s.end());     //删除一段内容,这里是全部删除

清空元素clear()文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

将整个set集合中的内容清空,注意,这里只是清空元素,其所占用的最大内存空间还是不会改版的。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

s.clear();

d) 查找元素find()文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

函数原型:iterator find (const value_type& val) const;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

cout<< s.find('d')->second <<endl;

或者 实现找到的删除指定元素文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html

s.erase(s.find('d'));
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/25125.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/25125.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定