STL map用法总结(multimap)
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)的更多相关文章
- c++ STL map 用法
map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据 处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时 ...
- STL map 用法
首先make_pair Pairs C++标准程序库中凡是"必须返回两个值"的函数, 也都会利用pair对象 class pair可以将两个值视为一个单元.容器类别map和mul ...
- STL:map用法总结
一:介绍 map是STL的关联式容器,以key-value的形式存储,以红黑树(平衡二叉查找树)作为底层数据结构,对数据有自动排序的功能.命名空间为std,所属头文件<map> 二:常用操 ...
- POJ2503 STL map用法
2017-08-21 15:42:01 writer:pprp 除了用到map以外,输入也是一个问题 用到了sscanf详情请看上一篇博客 /* theme:第一章 - 分治算法 name: POJ ...
- STL Map和multimap 容器
STL Map和multimap 容器 map/multimap的简介 map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供 基于key的快速检索能力. ...
- C++中的STL中map用法详解(转)
原文地址: https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html C++中的STL中map用法详解 Map是STL的一个关联容器,它提供 ...
- 2.9 C++STL map/multimap容器详解
文章目录 2.9.1 引入 2.9.2 代码示例 map案列 multimap案列 2.9.3 代码运行结果 总结 2.9.1 引入 map相对于set区别,map具有键值和实值,所有元素根据键值自动 ...
- POJ 3096 Surprising Strings(STL map string set vector)
题目:http://poj.org/problem?id=3096 题意:给定一个字符串S,从中找出所有有两个字符组成的子串,每当组成子串的字符之间隔着n字符时,如果没有相同的子串出现,则输出 &qu ...
- std::map用法
STL是标准C++系统的一组模板类,使用STL模板类最大的好处就是在各种C++编译器上都通用. 在STL模板类中,用于线性数据存储管理的类主要有vector, list, map 等等.本文主要 ...
随机推荐
- Vue1.x 到Vue2.0的一个变化
小弟初来乍到,写的不好的地方还望指正.谢谢各位! 废话不多说 进入正题: Vue1.x到2.0的一个变化 1. 在每个组件模板,不在支持片段代码 组件中模板: 之前: <templa ...
- 前端开发 - HTML
1.index2.head标签相关内容3.常用标签一4.常用标签二 table5.常用标签二 form6.标签分类 1.index <!--声明文档的类型 标记该文档为HTML5的文件--> ...
- 【opencv】imread 赋值 深拷贝浅拷贝
import cv2 import copy import os def filter_srcimg(dstimg): ss=3 srcimg=copy.deepcopy(dstimg) #aa=5 ...
- 【我的Android进阶之旅】Android目录过长造成错误:Failed to crunch file abc_textfield_search_activated_mtrl_alpha.9.png
一.编译异常描述 一大早来开发一个新的需求,拉取了一个新的分支,然后导入Android Studio之后,编译就报错了,报错如下所示: 错误具体日志如下所示: Information:Gradle t ...
- python基础之小数据池、代码块、编码
一.代码块.if True: print(333) print(666) while 1: a = 1 b = 2 print(a+b) for i in '12324354': print(i) 虽 ...
- decorators.xml的用法
spring,hibernate框架做的,对了,还有jQuery.我用jquery做异步请求到后台,生成json数据返回前台生成下拉输入框,请求到后台以后,成功生成了json数据并根据struts的映 ...
- js-jquery-002-条形码-一维码
一.使用 官方地址:http://barcode-coder.com/en/barcode-jquery-plugin-201.html 1.js引用 <script type="te ...
- .def文件如何编写
DLL中导出函数的声明有两种方式:一种为在函数声明中加上__declspec(dllexport),这里不再举例说明:另外一种方式是采用模块定义(.def) 文件声明. 规则是:1.首先创建 一个DL ...
- 测试:safenet提供的CheckKey函数 内存泄漏。具体来说是句柄.
unsigned char vendor_code[] = "7XSQT4jxlSkDJhwqpxxfLwbuxgrYw93OMy+K5sc5pyfTa7HQo1ikLyg7FDuEpgUK ...
- mysql数据库从删库到跑路之mysql完整性约束
一 介绍 约束条件与数据类型的宽度一样,都是可选参数 作用:用于保证数据的完整性和一致性主要分为: PRIMARY KEY (PK) 标识该字段为该表的主键,可以唯一的标识记录 FOREIGN KEY ...