2017-08-19 10:58:52

writer;pprp

#include <map>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm> using namespace std;
typedef pair<int, string> PAIR; ostream & operator<<(ostream & out, const PAIR& p)
{
out << p.first << " " << p.second << endl;
return out;
} //函数对象,对()进行了重载
struct CmpByValue
{
bool operator()(const PAIR&l, const PAIR&r)
{
return l.second < r.second;
}
}; int main()
{
//数据插入
map<int, string> mapStudent; mapStudent.insert(map<int, string>::value_type (, "student_one"));
mapStudent.insert(map<int, string>::value_type (, "student_two"));
mapStudent.insert(map<int, string>::value_type (, "student_three"));
mapStudent.insert(map<int, string>::value_type (,"student_four"));
mapStudent.insert(pair<int, string>(, "student_six"));
mapStudent.insert(make_pair(, "student_seven"));
mapStudent[] = "student_five"; //数据遍历
//正向遍历
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
//反向遍历
map<int, string>::reverse_iterator it;
for(it = mapStudent.rbegin(); it != mapStudent.rend() ; it++)
{
cout << it->first <<" " << it->second << endl;
}
//数组形式的遍历
int nSize = mapStudent.size();
for(int i = ; i < nSize ; i++)
{
cout << mapStudent[i] << endl;
}
cout << endl; //数据的查找
map<int, string>::iterator iter1; iter1 = mapStudent.find(); if(iter1 != mapStudent.end())
{
cout << iter1->second << endl;
}
else
{
cout << "can't find it " << endl;
} //判断是否存在这个key
int judge = mapStudent.count(); if(judge)
{
cout << "exist" << endl;
}
else
{
cout << "not exist" << endl;
} cout << endl; //用lower_bound进行查找
map<int, string> :: iterator it2;
it2 = mapStudent.lower_bound();
cout << it2->second << endl;
it2 = mapStudent.upper_bound();
cout << it2->second << endl; pair<map<int, string>::iterator, map<int, string>::iterator> mapair;
mapair = mapStudent.equal_range();
//如果相等的话那就说明没有找到,如果不等就说明找到了
if(mapair.first == mapair.second)
cout << "can't find it" << endl;
else
cout << "the value of it is " << mapair.first->second << endl; //数据的清空与判空
// mapStudent.clear();
if(!mapStudent.empty())
cout << "not empty" << endl;
else
cout << "empty" << endl; //数据的删除
map<int, string> :: iterator it3;
it3 = mapStudent.find();
mapStudent.erase(it3); for(it3 = mapStudent.begin(); it3 != mapStudent.end(); it3++)
cout << it3->second << endl; judge = mapStudent.erase();
if(judge)
{
cout << "delete successfully" << endl;
for(it3 = mapStudent.begin(); it3 != mapStudent.end(); it3++)
{
cout << it3->second << endl;
}
} //排序(按照key的大小排序)
//map<int, string, less<int> >是默认的升序
//map<int, string, greater<int> >是可以构造的降序 //排序(按照value的大小排序)
vector <PAIR> sortByValue(mapStudent.begin(), mapStudent.end()); sort(sortByValue.begin(), sortByValue.end(),CmpByValue()); for(int i = ; i < mapStudent.size(); i++)
{
cout << sortByValue[i]<< endl;
}
return ;
}

另外multimap用法与map类似,函数什么的都一样,只是支持一个key对多个value

/*
name : usage of List
writer : pprp
declare : null
date ; 2017/8/20
*/
#include <bits/stdc++.h> using namespace std; void print(multimap<string,double>&k)
{
multimap<string, double>::iterator it;
for(it = k.begin(); it != k.end() ; it++)
{
cout << it->first << " " << it->second << endl;
}
} int main()
{
multimap<string, double>mp;
mp.insert(pair<string,double>("jack",300.23));
mp.insert(make_pair("green",234.1));
mp.insert(make_pair("red",234.132));
mp.insert(make_pair("yellow",2342.1));
mp.insert(make_pair("blue",234.11));
mp.insert(make_pair("orange",2324.1)); multimap<string, double>::iterator it; print(mp); mp.erase("jack"); print(mp); it = mp.find("orange");
if(it != mp.end())
{
cout << it->first << " " << it->second << endl;
} return ;
}

STL map用法总结(multimap)的更多相关文章

  1. c++ STL map 用法

    map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据 处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时 ...

  2. STL map 用法

    首先make_pair Pairs C++标准程序库中凡是"必须返回两个值"的函数, 也都会利用pair对象  class pair可以将两个值视为一个单元.容器类别map和mul ...

  3. STL:map用法总结

    一:介绍 map是STL的关联式容器,以key-value的形式存储,以红黑树(平衡二叉查找树)作为底层数据结构,对数据有自动排序的功能.命名空间为std,所属头文件<map> 二:常用操 ...

  4. POJ2503 STL map用法

    2017-08-21 15:42:01 writer:pprp 除了用到map以外,输入也是一个问题 用到了sscanf详情请看上一篇博客 /* theme:第一章 - 分治算法 name: POJ ...

  5. STL Map和multimap 容器

    STL Map和multimap 容器 map/multimap的简介 map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供 基于key的快速检索能力.       ...

  6. C++中的STL中map用法详解(转)

    原文地址: https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html C++中的STL中map用法详解   Map是STL的一个关联容器,它提供 ...

  7. 2.9 C++STL map/multimap容器详解

    文章目录 2.9.1 引入 2.9.2 代码示例 map案列 multimap案列 2.9.3 代码运行结果 总结 2.9.1 引入 map相对于set区别,map具有键值和实值,所有元素根据键值自动 ...

  8. POJ 3096 Surprising Strings(STL map string set vector)

    题目:http://poj.org/problem?id=3096 题意:给定一个字符串S,从中找出所有有两个字符组成的子串,每当组成子串的字符之间隔着n字符时,如果没有相同的子串出现,则输出 &qu ...

  9. std::map用法

    STL是标准C++系统的一组模板类,使用STL模板类最大的好处就是在各种C++编译器上都通用.    在STL模板类中,用于线性数据存储管理的类主要有vector, list, map 等等.本文主要 ...

随机推荐

  1. 转载(web app变革之rem)

    rem这是个低调的css单位,近一两年开始崭露头角,有许多同学对rem的评价不一,有的在尝试使用,有的在使用过程中遇到坑就弃用了.但是我对rem综合评价是用来做web app它绝对是最合适的人选之一. ...

  2. 修改计算机名后SQLServer无法使用windows账号登录

    USE master     GO    EXEC sp_configure'allow updates',1 RECONFIGURE WITH OVERRIDE   -- 设置两个变量    DEC ...

  3. java7(1)——反编译深入理解增强的switch(读字节命令实战)

    [本文介绍] 本文主要讲java_7 的改进switch的底层实现.反编译一个使用带String的switch的demo并一步步解析反编译出来的字节命令,从编译的角度解读switch的底层实现. [正 ...

  4. OpenGL1.0 线段

    OpenGL1.0线段 DionysosLai2014-06-16          本篇作为我学习Opengl第一篇文档.希望自己能在图形学路上走得远一点,达到可以渲染游戏画质目的,现阶段是Box2 ...

  5. vultr服务器上搭建PHP运行环境-centos7

    安装nginx: yum install nginx 开启Nginx systemctl start nginx 访问你的域名或者ip 例如:quantaedu.com 安装PHP 查看系统版本lsb ...

  6. Latch free等待事件

    Latch free等待事件的三个参数:p1-latch的地址:p2-latch编号:p3-请求次数.从oracle10g起,latchfree不再包含所有的latch等待,有些latch等待可能表现 ...

  7. Java游戏服务器成长之路——感悟篇

    又是一个美好的周末啊,现在一到周末,早上就起得晚,下午困了又会睡一两个小时,上班的时候,早上起来喝一杯咖啡,然后就能高效的工作一整天,然而到了周末人就懒散了,哈哈. 最近刚跳槽,到新公司已经干了有两周 ...

  8. linux_rpm命令

    rpm 常用命令 1.安装一个包 # rpm -ivh 文件名 2.升级一个包 # rpm -Uvh文件名 3.移走一个包 # rpm -e文件名 4.安装参数 --force 即使覆盖属于其它包的文 ...

  9. URAL - 1091 Tmutarakan Exams (简单容斥原理)

    题意:K个不同数组成的集合,每个数都不超过S且它们的gcd>1.求这样的数的个数 分析:从2开始枚举gcd,但这样会发生重复.譬如,枚举gcd=2的集合个数和gcd=3的集合个数,枚举6的时候就 ...

  10. ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (set维护)

    注意题目保证不会有一个矩形完全包括另一个矩形的情况 时间序上从后往前看,一个坐标\((x,y)\)加进来之前,如果已经有\(x_i<x\),则对结果的贡献为\(x-x_i\);若不存在\(x_i ...