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. Mysql日常操作

    创建用户并授权 grant all privileges on test.* to "test"@"localhost" identified by " ...

  2. rabbitMQ 安装,集群搭建, 编码

    RabbitMQ 一.背景 命令行工具: http://www.rabbitmq.com/man/rabbitmqctl.1.man.html 介绍入门文章: http://blog.csdn.net ...

  3. django-vue之信息过滤(过滤课程)

    一  vue前端代码 实现的内容,通过对课程的分类,在每个不同的课程分类下显示相应的内容 <template> <div class="course"> & ...

  4. PushState+Ajax实现简单的单页面应用SPA

    http://www.helloweba.com/view-blog-386.html 单页面应用(Single Page Application)简称SPA,使用SPA构建的应用优点有用户体验好.速 ...

  5. linux下python编辑器的tab补全

    vi tab.py #!/usr/bin/env python # python startup file import sys import readline import rlcompleter ...

  6. Xcode8免证书生产IPA打包文件

    免证书生产IPA打包文件   修改Xcode配置文件: 关闭Xcode.然后打开“其他-终端”,就是命令行工具 cd /Applications/Xcode.app/Contents/Develope ...

  7. 国内NLP的那些人那些会

    统计学和语言学专家都列在一起了,没有区分.1,黄昌宁,1937年生于广东,1955年考入清华大学电机系,1961年毕业并留校任教至博士生导师, 1983-1984年赴美国耶鲁大学进修,1986-198 ...

  8. linux用户 群组权限

    用户及passwd文件 /etc/passwd文件的功能 /etc/passwd文件每个字段的具体含义 shadow文件 /etc/shadow文件的功能 /etc/shadow文件每个字段的具体含义 ...

  9. (C++) Assertion failed: !"Bad error code", file VMem.c, line 715

    (C++) Assertion failed: !"Bad error code", file VMem.c, line 715 Misc error. myInterface F ...

  10. 4. MySQL必知必会之排序检索数据-ORDER BY

    本章将讲授如何使用SELECT语句的ORDER BY子句,根据需要排序检 索出的数据. 1. 排序数据