map和multimap映射容器
map容器
map所处理的数据与数据库表具有键值的记录非常相似,在键值与映射数据之间,建立一个数学上的映射关系。map容器的数据结构仍然採用红黑树进行管理。插入的元素键值不同意反复,所使用的结点元素的比較函数仅仅对元素的键值进行比較,元素的各项数据能够通过键值检索出来。对于键值和映射数据。能够通过pair封装成一个结构对象。map要做的就是将这个pair对象插入到红黑树中,同一时候也须要提供一个仅使用键值进行比較的函数对象,将它传递给红黑树。此时就能够利用红黑树的操作将map元素插入到二叉树的正确位置。并对其进行元素的删除和检索。map与set的差别主要在于,map处理的是带有键值的记录型元素数据的高速插入、删除和检索,而set是对单一数据的处理。二者都是泛型库对二叉树的一个泛化。
创建map对象
主要有下面几种方式。
(1) map()
创建一个空的map对象,元素的键值类型为char,元素的映射数据类型为int,键值的比較函数对象为greater<char>
map<char,int,grater<char>> m;
(2) map(const key_compare&cmp)
struct strLess{
bool operator()(const char* s1,const char*s2)const
{
return strcmp(s1,s2)<0;
}
};
map<const char*,int> m(strLess());
(3) map(const map&)
拷贝构造函数。
map<int,char*> m1;
map<int,char*> m2(m1);
(4) map(InputIteratorfirst,InputIterator last)
pair<const int,char> p1(1,'a');
pair<const int,char> p2(2,'b');
pair<const int,char> p3(3,'c');
pair<const int,char> p4(4,'d');
pair<const int,char> array[]={p1,p2,p3,p4};
map<const int,char> m(array,array+4);
(5) map(InputIteratorfirst,InputIterator last, const key_compare&cmp)
map<const int,char,greater<const int>>m(array,array+4,greater<const int>());
元素的插入
元素的插入主要利用insert函数,利用数组操作也能够显式地为map赋值。可是不能检測是否插入成功。
#include<iostream>
#include<map>
using namespace std;
int main()
{
map<const char*,float> m;
m["apple"]=1.5f;
m["orange"]=2.0f;
m["banana"]=1.1f;
cout<<"苹果价格:"<<m["apple"]<<"元/斤"<<endl;
cout<<"橘子价格:"<<m["orange"]<<"元/斤"<<endl;
cout<<"香蕉价格:"<<m["banana"]<<"元/斤"<<endl;
return 0;
}
元素的删除
与set集合容器一样,map容器能够删除某个迭代器位置上的元素。等于某个键值的元素。一个迭代器区间上的元素和容器中的全部元素。erase函数和clear函数。
元素的遍历
map元素的遍历除了利用键值的数组方式来訪问元素,还能够使用迭代器的方式进行訪问。
#include<iostream>
#include<map>
using namespace std;
struct stuInfo{
char *name;
int age;
};
struct stuRecord{
int id;
stuInfo sf;
};
int main()
{
stuRecord sArray[]={{1,"li",20},{10,"shi",18},{3,"wang",21}};
map<int,stuInfo> m;
for(int i=0;i<3;i++)
{
m[sArray[i].id]=sArray[i].sf;
}
map<int,stuInfo>::iterator begin,end;
end=m.end();
cout<<"学号 "<<"姓名 "<<"年龄 "<<endl;
for(begin=m.begin();begin!=end;begin++)
{
cout<<(*begin).first<<" "
<<(*begin).second.name<<" "
<<(*begin).second.age<<" "
<<endl;
} return 0;
}
从结果能够看出,尽管是无序插入的,可是遍历出的结果是有序的。
元素的搜索
利用find函数能够搜索出具有某一键值的元素。
#include<iostream>
#include<map>
using namespace std;
struct stuRecord{
struct stuInfo{
char *name;
int age;
};
stuRecord(int id_,char *name_,int age_)
{
id=id_;
sf.name=name_;
sf.age=age_;
}
int id;
stuInfo sf;
}; int main()
{
typedef map<int,stuRecord::stuInfo> stuMap;
stuMap m;
pair<stuMap::iterator,bool> p;
//插入第一条学生记录
stuRecord stu1=stuRecord(5,"li",22);
pair<int,stuRecord::stuInfo> pairStu1(stu1.id,stu1.sf);
p=m.insert(pairStu1);
if(!p.second)
cout<<"插入学生记录失败\n";
//插入第二条学生记录
stuRecord stu2=stuRecord(1,"shi",20);
pair<int,stuRecord::stuInfo> pairStu2(stu2.id,stu2.sf);
p=m.insert(pairStu2);
if(!p.second)
cout<<"插入学生记录失败\n";
//插入第三条学生记录
stuRecord stu3=stuRecord(10,"zhang",21);
pair<int,stuRecord::stuInfo> pairStu3(stu3.id,stu3.sf);
p=m.insert(pairStu3);
if(!p.second)
cout<<"插入学生记录失败\n"; //搜索
stuMap::iterator i=m.find(5);
cout<<"搜索学号为5的记录:\n"
<<(*i).first<<' '
<<(*i).second.name<<' '
<<(*i).second.age<<' '<<endl; return 0;
}
map还提供其它的函数,empty、size、swap、lower_bound、upper_bound、equal_range等。
multimap多重映射容器
multimap容器也是用红黑树对记录型元素数据依照键值的比較关系进行高速的插入、删除、检索,元素的检索是对数级的时间复杂度,与map不同的是,multimap同意将具有反复键值的元素插入容器,元素的键值与元素的映射数据的映射关系是多对多的。
创建multimap对象
有下面几种方式。
(1) multimap()
multimap<char,int,greater<char>>mm;
(2) multimap(constkey_compare&cmp)
struct strLess{
bool operator()(const char *s1,const char*s2)const
{
return strcmp(s1,s2)<0;
}
};
multimap<constchar*,int> mm(strLess());
(3) multimap(const map&)
multimap<int,char*> mm1;
multimap<int,char*> mm2(mm1);
(4) multimap(InputIteratorfirst,InputIterator last)
pair<constint,char> p1(1,'a');
pair<constint,char> p2(1,'e');
pair<constint,char> p3(2,'b');
pair<constint,char> p4(3,'c');
pair<constint,char> pairArray[]={p1,p2,p3,p4};
multimap<constint,char> mm(pairArray,pairArray+4);
(5) multimap(InputIteratorfirst,InputIterator last, const key_compare&cmp)
multimap<constint,char,greater<const int>> mm(pairArray,pairArray+4,greater<constint>());
元素的插入
multimap容器仅仅能使用insert函数插入元素到容器的红黑树中。
multimap<float,char *> mm;
mm.insert(pair<float,char*>(3.0f,"apple"));
mm.insert(pair<float,char*>(3.0f,"pear"));
mm.insert(pair<float,char*>(2.1f,"orange"));
mm.insert(pair<float,char*>(1.5f,"banana"));
元素的删除
与map集合容器一样。map容器能够删除某个迭代器位置上的元素。等于某个键值的元素,一个迭代器区间上的元素和容器中的全部元素,erase函数和clear函数。
元素的遍历
multimap容器的遍历,能够用迭代器来实现。
#include<iostream>
#include<map>
using namespace std;
int main()
{
multimap<float,char *> mm;
mm.insert(pair<float,char *>(3.0f,"apple"));
mm.insert(pair<float,char *>(3.0f,"pear"));
mm.insert(pair<float,char *>(2.1f,"orange"));
mm.insert(pair<float,char *>(1.5f,"banana")); multimap<float,char *>::iterator begin,end;
end=mm.end();
for(begin=mm.begin();begin!=end;begin++)
{
cout<<(*begin).second<<" "<<(*begin).first<<"元/斤"<<endl;
}
cout<<endl;
return 0;
}
反向遍历
利用反向迭代器能够实现逆向遍历。
#include<iostream>
#include<map>
using namespace std;
int main()
{
multimap<float,char *> mm;
mm.insert(pair<float,char *>(3.0f,"apple"));
mm.insert(pair<float,char *>(3.0f,"pear"));
mm.insert(pair<float,char *>(2.1f,"orange"));
mm.insert(pair<float,char *>(1.5f,"banana")); multimap<float,char *>::reverse_iterator rbegin,rend;
rend=mm.rend();
for(rbegin=mm.rbegin();rbegin!=rend;rbegin++)
{
cout<<(*rbegin).second<<" "<<(*rbegin).first<<"元/斤"<<endl;
}
return 0;
}
元素的搜索
multimap容器的find函数将返回第一个搜索到的元素的位置,假设元素不存在将返回end结束元素位置。
其它经常使用函数
其它经常使用函数有empty、size、count、lower_bound、upper_bound等。
#include<iostream>
#include<map>
using namespace std;
int main()
{
multimap<int,char> mm;
cout<<mm.size()<<endl;
mm.insert(pair<int,char>(3,'a'));
mm.insert(pair<int,char>(3,'c'));
mm.insert(pair<int,char>(1,'b'));
mm.insert(pair<int,char>(2,'d'));
mm.insert(pair<int,char>(3,'e'));
mm.insert(pair<int,char>(4,'g')); cout<<mm.count(3)<<endl;
cout<<mm.size()<<endl; return 0;
}
map和multimap映射容器的更多相关文章
- STL(六)——map、multimap
STL--map.multimap 文章目录 STL--map.multimap 关联容器与map的介绍 map与set的异同 map与multimap的异同 map类对象的构造 map添加元素 ma ...
- STL学习系列九:Map和multimap容器
1.map/multimap的简介 map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供基于key的快速检索能力. map中key值是唯一的.集合中的元素按一定的顺 ...
- STL的基本使用之关联容器:map和multiMap的基本使用
STL的基本使用之关联容器:map和multiMap的基本使用 简介 map 和 multimap 内部也都是使用红黑树来实现,他们存储的是键值对,并且会自动将元素的key进行排序.两者不同在于map ...
- STL之Map和multimap容器
1.Map和multimap容器 1)map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供基于key的快速检索能力. 2)map中key值是唯一的.集合中的元素按一 ...
- C++ STL 学习笔记__(8)map和multimap容器
10.2.9 Map和multimap容器 map/multimap的简介 ² map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供基于key的快速检索能力. ² ...
- STL Map和multimap 容器
STL Map和multimap 容器 map/multimap的简介 map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供 基于key的快速检索能力. ...
- STL标准库-容器-map和multimap
技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 map与multimap为关联容器,结构如下 map底层实现依然是rb_tree 他的data可以改,但是key不能改,因此ma ...
- STL学习笔记— —容器map和multimap
简单介绍 在头文件<map> 中定义 namespace std { template <typename Key, typename T, typename Compare = l ...
- STL关联式容器之map和multimap
一,map和multimap的概念 1.map和multimap的基本知识 map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供基于key的快速检索能力. map中 ...
随机推荐
- Hadoop集群(第13期)_HBase 常用Shell命令
进入hbase shell console$HBASE_HOME/bin/hbase shell如果有kerberos认证,需要事先使用相应的keytab进行一下认证(使用kinit命令),认证成功之 ...
- 使用magento eav数据模型为用户提供图片上传功能的实践
一,在megento表中,增加一个存储上传图片路径的属性, 给magento的customer实体类型增加一个audit_file_path属性,因为要customer使用的是EAV模型,得操作几个关 ...
- 在WPF的MVVM框架中获取下拉选择列表中的选中项
文章概述: 本演示介绍怎样在WPF的MVVM框架中.通过数据绑定的方式获取下拉列表中的选中项.程序执行后的效果例如以下图所看到的: 相关下载(代码.屏幕录像):http://pan.baidu.com ...
- Android ViewGroup拦截触摸事件具体解释
前言 在自己定义ViewGroup中.有时候须要实现触摸事件拦截.比方ListView下拉刷新就是典型的触摸事件拦截的样例. 触摸事件拦截就是在触摸事件被parent view拦截,而不会分发给其ch ...
- hrift does not support polymorphic data types
hrift does not support polymorphic data types Exception in thread "main" com.facebook.swif ...
- iOS 相似QQ空间表视图下拉头部视图放大效果实现
UITableView 是 UIScrollView 的子类. 所以 UIScrollView 的代理方法.在UITableView 上相同可以得到适用. 既然如此那么我们就行知道.在表格下拉的过程中 ...
- xhprof安装&&使用
听同事说起过一个php性能分析扩展,叫xhprof,近期了解了下. XHProf 是一个轻量级的分层性能測量分析器. 在数据收集阶段.它跟踪调用次数与測量数据,展示程序动态调用的弧线图. 它在报告.后 ...
- JAVA实现远程SSH连接linux并运行命令
博客转移到http://blog.codeconch.com
- ITWorld:2014年全球最杰出的14位编程天才
近日,ITWorld 整理全球最杰出的 14 位程序员,一起来看下让我们膜拜的这些大神都有哪些?(排名不分先后) 1.Jon Skeet 个人名望:程序技术问答网站 Stack Overflow 总排 ...
- Android Studio配置GreenDAO 3.2.0和使用方法
我相信,在平时的开发过程中,大家一定会或多或少地接触到SQLite.然而在使用它时,我们往往需要做许多额外的工作,像编写SQL语句与解析查询结果等.所以,适用于Android ORM框架也就孕育而生了 ...