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 ...
随机推荐
- 构建 shiro struts2 spring3 mybatis 的maven项目
书接上回 构建 struts2 spring3 mybatis 的maven项目 构建 pom.xml 继续在原有框架下 融合shiro ,具体shiro是啥 这里就不解释了,恩 反正功能挺强大的 本 ...
- Boolean 布尔类型详解
这是最简单的类型.boolean 表达了真值,可以为 TRUE 或 FALSE.两个都不区分大小写. 要明确地将一个值转换成 boolean,用 (bool)或者 (boolean) 来强制转换.但是 ...
- osg(OpenSceneGraph)学习笔记1:智能指针osg::ref_ptr<>
OSG的智能指针,osg::ref_ptr<> osg::Referenced类管理引用计数内存块,osg::ref_ptr需要使用以它为基类的其它类作为模板参数. osg::ref_pt ...
- yarn资源管理器高可用性的实现
资源管理器高可用性 . The ResourceManager (RM) is responsible for tracking the resources in a cluster, and sch ...
- jsp调用javabean出现错误HTTP Status 500 - Unable to compile class for JSP
HTTP Status 500 - Unable to compile class for JSP: type Exception report message Unable to compile ...
- C语言学习笔记——堆和栈——未整理
C语言笔记 栈区 栈stack是一种先进后出的内存结构,所有的自动变量,函数的形参都是由编译器自动放出栈中,当一个自动变量超出其作用域时,自动从栈中弹出.出入栈是由C语言编译器自动分配 ...
- 丢沙包游戏(或杀人游戏)的C语言实现
丢沙包游戏(或杀人游戏)用C语言实现: 游戏简述: 杀人游戏(或者丢沙包游戏),设定一些人(人数为:num)一起玩游戏,从某个指定的人(设定为:start)开始轮流扔沙包,扔沙包人的下一个人为1,每隔 ...
- iOS Xcode制作模板类-b
为什么要定义模板类 遵守代码规范可以提高代码可读性, 降低后期维护成本. 当我们定下了一个团队都认同的代码规范, 如我们要求所有的viewController的代码都得按照下面来组织: #pragma ...
- the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header
the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header ...
- hdu 4442
一道超级easy的贪心 一眼看出了他的本质: 代码: #define mod 31536000 #include<cstdio> #include<algorithm> #in ...