stuff about set multiset map multimap
A lot of interviewers like to ask the candidates the difference between set and multiset(map and multimap).What does multi actually mean?Multi-container could have duplicate element.Check the code below.
/*********************************************
Author:Zhou You
Time:2014.09.06
Feature:comparison of containers such as set,multiset,map and multimap.
*********************************************/ #include <iostream>
#include <string>
#include <map>
#include <set> using namespace std; class setdata
{
public:
setdata():
data_(){
}
setdata(int data):
data_(data){
}
~setdata(){} bool operator<(const setdata &data) const{
return data_<data.GetData();
} int GetData() const{
return data_;
} private:
int data_;
}; struct mapcmp
{
bool operator()(const int &data1,const int &data2){
return data1<data2;
}
}; set<setdata> set_int;
multiset<setdata> multiset_int;
map<int,string,mapcmp> map_intstr;
multimap<int,string,mapcmp> multimap_intstr; int main()
{
//for set
set_int.insert(setdata());
set_int.insert(setdata());
set_int.insert(setdata());
set_int.insert(setdata()); cout<<"output set container data"<<endl;
for(set<setdata>::iterator iter = set_int.begin();iter!=set_int.end();++iter){
cout<<iter->GetData()<<" ";
}
cout<<endl<<endl; //for multiset
multiset_int.insert(setdata());
multiset_int.insert(setdata());
multiset_int.insert(setdata());//insert 20 again.
multiset_int.insert(setdata());
multiset_int.insert(setdata()); cout<<"output multiset container data"<<endl;
for(set<setdata>::iterator iter = multiset_int.begin();iter!=multiset_int.end();++iter){
cout<<iter->GetData()<<" ";
}
cout<<endl<<endl; //for map
map_intstr.insert(pair<int,string>(,"chen"));
map_intstr.insert(pair<int,string>(,"wang"));
map_intstr.insert(pair<int,string>(,"wu"));
map_intstr.insert(pair<int,string>(,"tang")); for(map<int,string>::iterator iter = map_intstr.begin();iter!=map_intstr.end();++iter){
cout<<iter->second<<" ";
}
cout<<endl<<endl; //for multimap
multimap_intstr.insert(pair<int,string>(,"chen"));
multimap_intstr.insert(pair<int,string>(,"wang"));
multimap_intstr.insert(pair<int,string>(,"wu"));
multimap_intstr.insert(pair<int,string>(,"wu"));//insert wu again.
multimap_intstr.insert(pair<int,string>(,"tang")); for(map<int,string>::iterator iter = multimap_intstr.begin();iter!=multimap_intstr.end();++iter){
cout<<iter->second<<" ";
}
cout<<endl; return ;
}
In both map and set,I define compare function myself.
stuff about set multiset map multimap的更多相关文章
- STL set multiset map multimap unordered_set unordered_map example
		
I decide to write to my blogs in English. When I meet something hard to depict, I'll add some Chines ...
 - STL:map/multimap用法详解
		
map/multimap 使用map/multimap之前要加入头文件#include<map>,map和multimap将key/value当作元素,进行管理.它们可根据key的排序准则 ...
 - STL之六:map/multimap用法详解
		
转载于:http://blog.csdn.net/longshengguoji/article/details/8547007 map/multimap 使用map/multimap之前要加入头文件# ...
 - STL中的map/multimap小结
		
(1)使用map/multimap之前必须包含头文件<map>:#include<map> 并且和所有的关联式容器一样,map/multimap通常以平衡二叉树来完成 (2)n ...
 - C++ map multimap
		
map multimap map,multimap key-value对容器,也叫字典,map中不能存放key相同的元素,而multimap可以,容器中元素默认按升序排序 map multimap的相 ...
 - iBinary C++STL模板库关联容器之map/multimap
		
目录 一丶关联容器map/multimap 容器 二丶代码例子 1.map的三种插入数据的方法 3.map集合的遍历 4.验证map集合数据是否插入成功 5.map数据的查找 6.Map集合删除元素以 ...
 - 09--STL关联容器(map/multimap)
		
一:map/multimap的简介 map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供基于key的快速检索能力. map中key值是唯一的.集合中的元素按一定的顺 ...
 - c/c++ 标准库 map multimap元素访问
		
标准库 map multimap元素访问 一,map,unordered_map下标操作 下标操作种类 功能描述 c[k] 返回关键字为k的元素:如果k不在c中,添加一个关键字为k的元素,并对其初始化 ...
 - STL之map&multimap使用简介
		
map 1.insert 第一种:用insert函数插入pair数据 #include <map> #include <string> #include <iostrea ...
 
随机推荐
- SQL优化(2)
			
建表时候数据库引擎的选择也可以达到优化的效果 InnoDB: 基于磁盘的资源是InnoDB表空间数据文件和它的日志文件,InnoDB 表的大小只受限于操作系统文件的大小,一般为 2GB MyISAM: ...
 - 关于Linux内核学习的误区以及相关书籍介绍
			
http://www.hzlitai.com.cn/article/ARM9-article/system/1605.html 写给Linux内核新手-关于Linux内核学习的误区 先说句正经的:其实 ...
 - jquery中onclick="fn"中$(this)所代表的对象
			
jquery中onclick="fn"中$(this)所代表的对象 js方法 function qiehuan(){ var src = $(this).attr("da ...
 - Linux grep和find的区别
			
这是两个不同的命令,关于grep:Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expressi ...
 - 解决php的$美元符号与Zen Coding冲突问题
			
Zen Coding插件就不介绍了. 众所周知,安装了插件以后,输入$符号会被自动解析为相应的数字1.2.3... 作为一名PHP程序员,想要通过其定义一些自己常用的代码.却发现展开以后悲剧的发现$符 ...
 - pyes-elasticsearch的python客户端使用笔记
			
elasticsearch入门: http://www.qwolf.com/?p=1387 一.重要的概念 http://834945712.iteye.com/blog/1915432 这篇文章很 ...
 - 写一个TT模板自动生成spring.net下面的配置文件。
			
这个是目标. 然后想着就怎么开始 1.
 - delphi xe5 android  开发数据访问手机端(一)
			
上几片文章我们把供手机端调用的web服务完成,接下来实现手机端调用webservices获取数据 1.新建firemonkey mobile application 2.选择blank applica ...
 - NOIP 2011 提高组 计算系数
			
有二项式定理 `\left( a+b\right) ^{n}=\sum _{r=0}^{n}\left( \begin{matrix} n\\ r\end{matrix} \right) a^{n-r ...
 - ArrayList与LinkedList实现比较
			
1.ArrayList实现是基于数组来实现的,这可由ArrayList的源码看出: public class ArrayList<E> extends AbstractList<E& ...