#include<iostream>
#include<string>
#include<map>
using namespace std; struct SelfType{
int number;
}; void map_find()
{
//map.lower_bound(keyElem);//返回第一个key>=keyElem元素的迭代器
//map.upper_bound(keyElem);//返回第一个key>keyElem元素的迭代器
//map.equal_range(keyElem);//返回一个pair,
//pair.first为第一个key>=keyElem元素的迭代器
//pair.second为第一个key>keyElem元素的迭代器 map<int,string> mapStu;
//1、通过pair的方式插入对象
//mapStu.insert( pair<int,string>(3, "小张"));
//2、通过value_type的方式插入对象
mapStu.insert( map<int, string>::value_type(, "小李") );
//3、通过数组的方式插入值
mapStu[] = "小张";
mapStu[] = "小刘";
mapStu[] = "小赵";
mapStu[] = "小王"; map<int,string>::iterator it_lower,it_upper;
it_lower=mapStu.lower_bound();
it_upper=mapStu.upper_bound();
cout<<it_lower->first<<","<<it_lower->second<<endl;
cout<<it_upper->first<<","<<it_upper->second<<endl;
pair<map<int,string>::iterator,map<int,string>::iterator> mapStuPair=mapStu.equal_range();
it_lower=mapStuPair.first;
it_upper=mapStuPair.second;
cout<<it_lower->first<<","<<it_lower->second<<endl;
if(it_upper !=mapStu.end())
{
cout<<it_upper->first<<","<<it_upper->second<<endl;
}
else
{
cout<<"it_upper ==mapStu.end()"<<endl;
}
/*
3,小刘
4,小赵
5,小王
it_upper ==mapStu.end()
请按任意键继续. . .
*/
} void map_greater()
{
map<int,string,greater<int>> mapStu;
//1、通过pair的方式插入对象
mapStu.insert( pair<int,string>(, "小张"));
//2、通过value_type的方式插入对象
mapStu.insert( map<int, string>::value_type(, "小李") );
//3、通过数组的方式插入值
mapStu[] = "小刘";
mapStu[] = "小王"; map<int,string,greater<int>>::iterator it;
for(it=mapStu.begin();it !=mapStu.end();++it)
{
cout<<it->first<<","<<it->second<<" ";
}
cout<<endl; }
void main()
{
map_find();return;
map_greater();return; map<int, char> mapA;
map<string, float> mapB;
//其中T1,T2还可以用各种指针类型或自定义类型
map<float, char> mapC;
map<int*, float> mapD;
map<SelfType, char> mapE; map<int,string> mapStu;
//1、通过pair的方式插入对象
mapStu.insert( pair<int,string>(, "小张"));
//2、通过value_type的方式插入对象
mapStu.insert( map<int, string>::value_type(, "小李") );
//3、通过数组的方式插入值
mapStu[] = "小刘";
mapStu[] = "小王"; map<int,string>::iterator it;
for(it=mapStu.begin();it !=mapStu.end();++it)
{
cout<<it->first<<","<<it->second<<" ";
}
cout<<endl; string strName = mapStu[];//取操作
cout<<strName.length()<<endl;
//只有当mapStu存在2这个键时才是正确的取操作,否则会自动插入一个实例,键为2,值为初始化值。
for(it=mapStu.begin();it !=mapStu.end();++it)
{
cout<<it->first<<","<<it->second<<" ";
}
cout<<endl; }
#include <iostream>
#include <string>
#include <map>
using namespace std; int main()
{
multimap<string,int> m_map;
string s("中国"),s1("美国");
m_map.insert(make_pair(s,));
m_map.insert(make_pair(s,));
m_map.insert(make_pair(s,));
m_map.insert(make_pair(s1,));
m_map.insert(make_pair(s1,));
m_map.insert(make_pair(s1,));
//方式1
int k;
multimap<string,int>::iterator m;
m = m_map.find(s);
for(k = ;k != m_map.count(s);k++,m++)
cout<<m->first<<"--"<<m->second<<endl;
//方式2
multimap<string,int>::iterator beg,end;
beg = m_map.lower_bound(s1);
end = m_map.upper_bound(s1);
for(m = beg;m != end;m++)
cout<<m->first<<"--"<<m->second<<endl;
//方式3
beg = m_map.equal_range(s).first;
end = m_map.equal_range(s).second;
for(m = beg;m != end;m++)
cout<<m->first<<"--"<<m->second<<endl;
return ;
} /*
中国--50
中国--55
中国--60
美国--30
美国--20
美国--10
中国--50
中国--55
中国--60
请按任意键继续. . .
*/

map_multimap的更多相关文章

随机推荐

  1. eclipse项目有红色感叹号

    1.依赖有问题 找到build path 点击configure build path 点击libraries 找到有红叉的包,然后选中移除 2.引用的包有问题 利用maven引用的包出现问题,有的包 ...

  2. [转帖]Docker公司被收购,开源界尴尬不?

    Docker公司被收购,开源界尴尬不? https://news.51cto.com/art/201911/606189.htm Docker公司被谁收了? Docker公司被谁收了?Mirantis ...

  3. 池化技术之Java线程池

     https://blog.csdn.net/jcj_2012/article/details/84906657 作用 线程池,通过复用线程来提升性能; 背景 线程是一个操作系统概念.操作系统负责这个 ...

  4. LeetCode 5214. 最长定差子序列(Java)HashMap

    题目: 5214. 最长定差子序列 给你一个整数数组 arr 和一个整数 difference,请你找出 arr 中所有相邻元素之间的差等于给定 difference 的等差子序列,并返回其中最长的等 ...

  5. 基于STM32F767两路互补SPWM波(HAL库)

    SPWM波指的是占空比呈正弦规律变化的PWM波,生成方式是在定时器中断中调整PWM波的占空比. 对于互补的两路SPWM波,一路为低电平 ‘0’ 时,另一路为高电平 ‘1’,即两路是互补的. 对于STM ...

  6. Dubbo使用javassist生成动态类

    在服务(本地和远程)暴露的时候会调用proxyFactory.getInvoker方法 具体位置: 本地暴露:ServiceConfig#exportLocal line:538 远程暴露: Serv ...

  7. win10下更新anaconda和pip源

    第一步:更新anaconda源. anaconda的官方源太慢,推荐清华源:https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/ 软件下载也可以在这个 ...

  8. java之spring mvc之ajax

    1.可以使用servletAPI来实现 ajax Controller 类 @Controller public class AjaxController { @RequestMapping(&quo ...

  9. JVM性能优化--Java的垃圾回收机制

    一.Java内存结构 1.Java堆(Java Heap) java堆是java虚拟机所管理的内存中最大的一块,是被所有线程共享的一块内存区域,在虚拟机启动时创建.此内存区域的唯一目的就是存放对象实例 ...

  10. 关于 Safari 浏览器不支持 location [ window.location.href window.open()] 跳转问题的解决方案

    最近在做项目时,碰到 safari 浏览器不支持location跳转问题,针对此问题,可以通过 js 模拟点击时间来解决,代码如下: <!DOCTYPE HTML> <html la ...