标准库 map multimap元素访问

一,map,unordered_map下标操作

下标操作种类 功能描述
c[k] 返回关键字为k的元素;如果k不在c中,添加一个关键字为k的元素,并对其初始化
c.at(k) 访问关键字为k的元素;若k不在c中,抛出out_of_range异常

### 注意:

1,当使用使用自定义类作为key时,这个类必须重写operator<函数

2,下标操作只适用于const map,unordered_map

二,访问元素

查找元素的操作 功能描述
c.find(k) 返回一个迭代器,指向第一个关键字为k的元素,若k不在c种,则返回c.end()
c.count(k) 返回关键字等于k的元素的数量。
c.lower_bound(k) 返回一个迭代器,指向第一个关键字大于等于k的元素。若k不在c中,返回和c.upper_bound(k)相等的迭代器。
c.upper_bound(k) 返回一个迭代器,指向第一个关键字大于k的元素。若k不在c中,返回和c.lower_bound(k)相等的迭代器。
c.equal_range(k) 返回一个pair,pair里面是2个c的迭代器。first为第一个关键字等于k的迭代器,second为最后一个关键字等于k的位置的下一个位置的迭代器。若未找到,则pair的2个成员都等于c.end()

小例子向导:

程序块 功能描述
test1 map的下标操作
test2 map 用自定义类型的下标操作
test3 map的查找
test4 multimap的查找

小例子:

#include <iostream>
#include <map>
#include <unordered_map>
#include <set>
#include <vector> using namespace std; class Test{
public:
Test(int d = 0):data(d){}
bool operator<(const Test& s)const{
return s.data < data;
}
const int& getData()const{
return data;
}
private:
int data;
};
int main(){
//test1 map的下标操作
/*
map<string,int> smap{{"aa",12},{"bb",10}};
unordered_map<int, int> imap{{1,11},{2,22}};
map<string,int>::mapped_type m1 = smap["aa"];//m1为int
cout << m1 << endl;
unordered_map<string,int>::mapped_type m2 = imap[2];//m2为int
cout << m2 << endl;
smap["aa"] = 33;
cout << smap["aa"] << endl;
smap["cc"] = 13;//想smap添加{"cc",13}
cout << smap["cc"] << endl;
cout << smap.at("cc") << endl;
//cout << smap.at("ccd") << endl;//抛出out_of_range异常
map<string,int>::mapped_type m3 = smap.at("aa");
cout << m3 << endl;
//想smap里添加了{"dd", 0},
cout << smap["dd"] << endl;
for(auto const &s : smap){
cout << s.first << "," << s.second << endl;
}
*/ //test2 map 用自定义类型的下标操作
/*
map<Test,int> tmap{{Test(10), 10},{Test(11), 11}};
tmap[Test()] = 1;
for(auto const &s : tmap){
cout << s.first.getData() << "," << s.second << endl;
}
*/ //test3 map的查找
/*
map<int, int> imap{{1,1},{3,3},{2,2},{5,5},{4,4}};
map<int,int>::iterator it1 = imap.find(1);
cout << it1->first << endl;
map<int,int>::iterator it2 = imap.find(4);//返回imap.end()
if(it2 == imap.end()){cout << "it2 is end" << endl;}
cout << imap.count(2) << endl;
auto it3 = imap.lower_bound(2);//{2,2}
cout << it3->first << "," << it3->second << endl;
auto it4 = imap.upper_bound(4);//{5,5}
cout << it4->first << "," << it4->second << endl;
*/ //test4 multimap的查找
multimap<string, string> autrs{{"aaa","n1"},{"bbb","n1"},{"aaa","n2"},
{"aaa","n3"}};
string sch("aaa");
//方法1
auto cnt = autrs.count(sch);
auto it = autrs.find(sch);
while(cnt){
cout << it->second << endl;
++it;
--cnt;
}
cout << "-----------------" << endl;
//方法2
for(auto beg = autrs.lower_bound(sch),end = autrs.upper_bound(sch);
beg != end; ++beg){
cout << beg->second << endl;
}
cout << "-----------------" << endl;
//方法3
for(auto pos = autrs.equal_range(sch);pos.first != pos.second;++pos.first){
cout << pos.first->second << endl;
}
}

github完整代码

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

c/c++ 标准库 map multimap元素访问的更多相关文章

  1. c/c++ 标准库 map set 大锅炖

    标准库 map set 大锅炖 一,关联容器有哪些 按关键字有序保存元素 map 保存key和value set 只保存key mulutimap key可以重复出现 multiset key可以重复 ...

  2. c/c++ 标准库 map set 删除

    标准库 map set 删除 删除操作 有map如下: map<int, size_t> cnt{{2,22}, {3,33}, {1,11}, {4,44}; 删除方法: 删除操作种类 ...

  3. c/c++ 标准库 map set 插入

    标准库 map set 插入 一,插入操作 有map如下: map<string, size_t> cnt; 插入方法: 插入操作种类 功能描述 cnt.insert({"abc ...

  4. C++之标准库map

    目录 1.成员函数 2.元素访问 3.迭代器Iterators(C++ 11) 4.容量Capacity 5.修改函数(C++ 11和C++ 17) 6.查找表Lookup 7.观察Observers ...

  5. C++ Primer 有感(标准库map类型)

    map是键-值对的集合.map类型通常可以理解为关联数组:可以使用键作为下标获取一个值,正如内置数组一样.而关联的本质在于元素的值于某个特定的键相关联,而并非通过元素在数组中的位置获取. 1.map对 ...

  6. C++ Primer 有感(标准库vector及迭代器)

    vector是同一种对象的集合,每个对象都有一个对应的整数索引值.和string对象一样,标准库将负责管理与存储元素相关的类存.引入头文件 #include<vector> 1.vecto ...

  7. C++标准库vector以及迭代器

    今天看C++的书,出现了一个新的概念,容器vector以及容器迭代器. vector是同一种对象的集合,每个对象都有一个对应的整数索引值.和string对象一样,标准库将负责管理与存储元素相关的类存. ...

  8. C++标准库vector及迭代器

    vector是同一种对象的集合,每个对象都有一个对应的整数索引值.和string对象一样,标准库将负责管理与存储元素相关的类存.引入头文件 #include<vector> 1.vecto ...

  9. Python 标准库、第三方库

    Python 标准库.第三方库 Python数据工具箱涵盖从数据源到数据可视化的完整流程中涉及到的常用库.函数和外部工具.其中既有Python内置函数和标准库,又有第三方库和工具.这些库可用于文件读写 ...

随机推荐

  1. 比MySQL快6倍 深度解析国内首个云原生数据库POLARDB的“王者荣耀”

    随着移动互联网.电子商务的高速发展,被使用最多的企业级开源数据系统MySQL面临着巨大挑战——为迎接“双11"的高并发要提前做好分库分表;用户不断激增要将读写分离才能应对每天上亿次的访问,但 ...

  2. RabbitMQ系列(四)RabbitMQ事务和Confirm发送方消息确认——深入解读

    RabbitMQ事务和Confirm发送方消息确认--深入解读 RabbitMQ系列文章 RabbitMQ在Ubuntu上的环境搭建 深入了解RabbitMQ工作原理及简单使用 RabbitMQ交换器 ...

  3. Qt 编程中 namespace Ui { class Widget; } 解析

    class Widget 里面有个声明 Ui::Widget *ui,这个 ui 是使用 namespace Ui 里的 Widget 类声明的,该类只是简单的继承了 ui_widget.h 里的 U ...

  4. JavaScript 系列博客(三)

    JavaScript 系列博客(三) 前言 本篇介绍 JavaScript 中的函数知识. 函数的三种声明方法 function 命令 可以类比为 python 中的 def 关键词. functio ...

  5. HttpClient之可恨的Expect(C# http 请求卡住的解决办法)

    今天用HTTP.HttpClient这个对象开发的时候遇到一个奇怪的问题 当POST一个页面的时候始终卡住提交不成功 最初以为协议有错误就抓包测试在抓包在测试 最后想到是不是HttpClient的BU ...

  6. 怎么使用小程序的data-*属性?

    参考文档:小程序事件 怎么使用小程序的data-*属性?[data-type,data-num,……] dataset 在组件中可以定义数据,这些数据将会通过事件传递给 SERVICE. 书写方式: ...

  7. CIL中间语言浅谈

    CIL中间语言 通用中间语言(Common Intermediate Language,简称CIL)(曾经被称为微软中间语言或MSIL)是一种属于通用语言架构和.NET框架的低阶(lowest-lev ...

  8. redirection in linux

    2>&1 # Redirects stderr to stdout. # Error messages get sent to same place as standard output ...

  9. Request method 'POST' not supported错误和解决方法

    在使用SpringBoot的时候,在html页面用form表单post提交数据的时候报错: Request method 'POST' not supported 错误解析: 我是用的前端页面是HTM ...

  10. Angular6 项目开发常用时间组件服务

    一.利用Angular 命令行工具生成一个服务. 详情见:<Angular环境搭建>,服务代码如下: import { Injectable } from '@angular/core'; ...