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. Yii框架2.0的模型

    模型是 MVC 模式中的一部分, 是代表业务数据.规则和逻辑的对象. 可通过继承 [[yii\base\Model]] 或它的子类定义模型类,基类[[yii\base\Model]]支持许多实用的特性 ...

  2. 剑指Offer——二叉搜索树的第k个结点

    题目描述: 给定一颗二叉搜索树,请找出其中的第k大的结点. 例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4 分析: 二叉搜索树中序遍历就是从小到大.只 ...

  3. golang处理 json中非法字符

    原文: Hi there, I just discovered Go and decided to port a little program to Go. The program reads JSO ...

  4. Flask用Flask-SQLAlchemy连接MySQL

    安装 pip3 install Flask-SQLAlchemy 测试环境目录结构 settings.py DIALECT = 'mysql' DRIVER = 'pymysql' USERNAME ...

  5. 浅析Android View(二)

    深入理解Android View(一) View的位置參数信息 二.View的绘制过程 View的绘制过程一共分为三个部分: - measure(測量View的大小) - layout(确定View的 ...

  6. 启动tomcat时为tomcat指定JDK

    背景:服务器环境:JDK1.7,Tomcat8 需求: 需要在Tomcat8部署项目,该项目需要运行在JDK1.8 将Tomcat8和JDK1.8上传至服务器,然后解压在指定目录下. 为tomcat指 ...

  7. JavaScript工具库 lodash 中文文档 英文文档

    https://lodash.com/docs/    英文版 http://lodashjs.com/docs/   中文版 http://www.css88.com/doc/lodash/ 中文版 ...

  8. 最新zencart支付宝插件(支持1.5)

    最新zencart支付宝插件(支持1.5) 最新zencart支付宝插件(支持1.5)   支付宝接口的兼容性真不错,时至今日还能用,想利用zencart来做国内时长还真是方便多了,朋友们可以试试. ...

  9. zen-cart 一页支付实现方法

    1.下载插件CSS JS Loader 和 Fast and Easy Checkout for Zen Cart,插件请下载附件 2.先把CSS JS Loader覆盖,后台选项点击,点击后,程序会 ...

  10. CCF 炉石传说(模拟)

    试题编号: 201612-3 试题名称: 炉石传说 时间限制: 1.0s 内存限制: 256.0MB 问题描述 <炉石传说:魔兽英雄传>(Hearthstone: Heroes of Wa ...