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 等等.本文主要 ...
随机推荐
- Gunner II--hdu5233(map&vector/二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5233 题意:有n颗树,第 i 棵树的高度为 h[i],树上有鸟,现在这个人要打m次枪,每次打的高度是 ...
- ansible(2)
一.ansible模块(yum.pip.service.conr.user.group) 上篇中我们已经学了ansible的几个模块,接下来再来学习几个,那么你是否知道ansible一共有多少模块呢? ...
- vim文本编辑操作
文本选择操作 为了方便地选取文本块,Vim编辑器引入了可视模式(Visual Mode).要选取一段文本块,操作步骤如下: ▶ 将光标移动到要复制文本块的开始处.要注意的是 ...
- Python(线程进程3)
四 协程 协程,又称微线程,纤程.英文名Coroutine.一句话说明什么是线程:协程是一种用户态的轻量级线程. 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到其他地方,在切 ...
- C# 使用windows服务发送邮件
最近做了一个使用 C# 写了一个发送邮件的 windows 服务,在这里记录一下. 首先使用 Visual Studio 2015 创建一个 windows 服务项目. 然后在设计器上面右击添加安装程 ...
- PL/SQL编程—存储过程
SQL> create or replace procedure sp_pro3(name_in varchar2,id_in varchar2) is begin update mytest ...
- Python3 计算城市距离
利用上一篇得到的城市经纬度算城市距离 import requests from math import radians, cos, sin, asin, sqrt def geocode(addres ...
- 如何获取iClap的内测资格
iClap,一款拥有智能产品管理能力的系统,第一次遇见是在8月下旬的创新中国的展会上,茫茫人海中只因多看了你一眼,便深深的留在脑海里挥之不去,展会结束的当天就忍不住想要更多的了解你,登陆iClap官网 ...
- 有关RDD的基础学习1
1.spark rdd为什么不能嵌套? 譬如 val rdd1=sc.parallel(range(1,100)) val rdd2=sc.parallel(range(1,100)) ...
- C#打印类
using System;using System.Collections.Generic;using System.Text;using System.Windows.Forms;using Sys ...