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的更多相关文章

  1. 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 ...

  2. STL:map/multimap用法详解

    map/multimap 使用map/multimap之前要加入头文件#include<map>,map和multimap将key/value当作元素,进行管理.它们可根据key的排序准则 ...

  3. STL之六:map/multimap用法详解

    转载于:http://blog.csdn.net/longshengguoji/article/details/8547007 map/multimap 使用map/multimap之前要加入头文件# ...

  4. STL中的map/multimap小结

    (1)使用map/multimap之前必须包含头文件<map>:#include<map> 并且和所有的关联式容器一样,map/multimap通常以平衡二叉树来完成 (2)n ...

  5. C++ map multimap

    map multimap map,multimap key-value对容器,也叫字典,map中不能存放key相同的元素,而multimap可以,容器中元素默认按升序排序 map multimap的相 ...

  6. iBinary C++STL模板库关联容器之map/multimap

    目录 一丶关联容器map/multimap 容器 二丶代码例子 1.map的三种插入数据的方法 3.map集合的遍历 4.验证map集合数据是否插入成功 5.map数据的查找 6.Map集合删除元素以 ...

  7. 09--STL关联容器(map/multimap)

    一:map/multimap的简介 map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供基于key的快速检索能力. map中key值是唯一的.集合中的元素按一定的顺 ...

  8. c/c++ 标准库 map multimap元素访问

    标准库 map multimap元素访问 一,map,unordered_map下标操作 下标操作种类 功能描述 c[k] 返回关键字为k的元素:如果k不在c中,添加一个关键字为k的元素,并对其初始化 ...

  9. STL之map&multimap使用简介

    map 1.insert 第一种:用insert函数插入pair数据 #include <map> #include <string> #include <iostrea ...

随机推荐

  1. [C#]『Task』任务并行库使用小计

    1.简单创建使用 using System; using System.Diagnostics; using System.Threading; using System.Threading.Task ...

  2. Quartz 之 windowService

    (一)创建服务 QuarzService using System.ServiceProcess;using System.Text; using Quartz;using Quartz.Impl; ...

  3. 数据库创建&数据表创建

    --第2_1题创建数据库 create database Student201038897041 on primary (name='student1', filename='F:\coures\SQ ...

  4. 【web安全】第三弹:web攻防平台pentester安装及XSS部分答案解析

    web for pentester是国外安全研究者开发的的一款渗透测试平台,通过该平台你可以了解到常见的Web漏洞检测技术. 下载链接及文档说明: http://pentesterlab.com/ex ...

  5. OC 之 const

    1. 修饰变量 一般设置传参数的时候 若设置为const, 则在调用过程中不允许修改参数值;(readonly) // *前const: 不能通过指针, 改变p指向的值 const int *p = ...

  6. PhoneGap 3 在 Mac 上安装使用

    1.下载安装 NodeJS . 2.安装 PhoneGap.打开终端执行: 1 $ sudo npm install -g phonegap 3.PhoneGap 3 不需要在Xcode中创建,而是在 ...

  7. IOS中获取文件路径的方法

    iphone沙箱模型的有四个文件夹,分别是什么,永久数据存储一般放在什么位置,得到模拟器的路径的简单方式是什么. documents,tmp,app,Library. (NSHomeDirectory ...

  8. REST响应处理

    JAX-RS 2.0 支持4种返回值类型的响应,分别是无返回值.返回Response类实例.返回GenericEntity类实例和返回自定义类实例. 1.在返回值类型是VOID的响应中,其响应实体为空 ...

  9. 解决CAS单点登录出现PKIX path building failed的问题

    在一次调试中,出现了这个错误: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderExceptio ...

  10. Android如何在一个线性布局里完美显示两个listview啊?

    复写一个listView ,在你布局文件中使用此view: <ScrollView android:layout_width="fill_parent" android:la ...