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. 【转】Oracle job procedure 存储过程定时任务

    原文:Oracle job procedure 存储过程定时任务 oracle job有定时执行的功能,可以在指定的时间点或每天的某个时间点自行执行任务. 一.查询系统中的job,可以查询视图 --相 ...

  2. HTML css面试题

    1.对WEB标准以及W3C的理解与认识 标签闭合.标签小写.不乱嵌套.提高搜索机器人搜索几率.使用外链css和js脚本.结构行为表现的分离.文件下载与页面速度更快.内容能被更多的用户所访问.内容能被更 ...

  3. 『奇葩问题集锦』Fedora ubuntu 下使用gulp 报错 Error: watch ENOSPC 解决方案

    用gulp启动,错误如下 Error: watch ENOSPC at exports._errnoException (util.js:746:11) at FSWatcher.start (fs. ...

  4. sudo: unable to resolve host XXX 解决方法

    执行sudo命令时候,总是提示sudo: unable to resolve host xxx 解决方法: 法1. 在/etc/hosts/添加hosts映射, 如127.0.0.1 xxx 法2. ...

  5. php 接收二进制流转换成图片

    php 接收二进制流转换成图片,图片类imageUpload.php如下: <?php /** * 图片类 * @author http://blog.csdn.net/haiqiao_2010 ...

  6. ASP.NET注意事项

    1.服务器上bin目录下面的dll备份的时候,第一个点号之前的名字不能是一样的,否则会报错. 2.

  7. 【web安全】第五弹:burpsuite proxy模块的一些理解

    作为一只小小小白的安全新手,只会简单的用sqlmap扫扫网站,用burpsuite的proxy模块拦截一些请求.最近又对proxy有点儿小理解,记录之. 1. 查看sqlmap注入的语句以及HTTP ...

  8. UIWebView1-b

    随着H5的强大,hybrid app已经成为当前互联网的大方向,单纯的native app和web app在某些方面显得就很劣势.关于H5的发展史,这里有一篇文章推荐给大家,今天我们来学习最基础的基于 ...

  9. 解决方案:安装wordpress出现500 Internal Server Error

    做一个资讯站点的时候遇到一个wordpress不知道算不算常见的问题:程序安装的时候提示500 Internal Server Error 那么最终百度谷歌找到以下解决方案: 安装新版本wordpre ...

  10. 50个实用的jQuery代码段让你成为更好的Web前端工程师

    本文会给你们展示50个jquery代码片段,这些代码能够给你的javascript项目提供帮助.其中的一些代码段是从jQuery1.4.2才开始支持的做法,另一些则是真正有用的函数或方法,他们能够帮助 ...