c/c++ 标准库 map multimap元素访问

时间:2022-09-07 10:32:56

标准库 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

c/c++ 标准库 map multimap元素访问

本人微信:xiaoshitou5854

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

  1. c&sol;c&plus;&plus; 标准库 map set 大锅炖

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

  2. c&sol;c&plus;&plus; 标准库 map set 删除

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

  3. c&sol;c&plus;&plus; 标准库 map set 插入

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

  4. C&plus;&plus;之标准库map

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

  5. C&plus;&plus; Primer 有感(标准库map类型)

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

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

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

  7. C&plus;&plus;标准库vector以及迭代器

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

  8. C&plus;&plus;标准库vector及迭代器

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

  9. Python 标准库、第三方库

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

随机推荐

  1. 个人阅读作业——M1&sol;M2总结

    ~ http://www.cnblogs.com/wx1306/p/4831950.html 在这篇博客中,我提出来一些关于软件工程的问题,但随着这一个学期的即将结束,以及我对软件开发的了解的深入,我 ...

  2. Request&sol;Server的相关topic

    Request---------Server模式 HTTP 协议--------->这个可能返回json, 也可能是HTML HTML页面处理的流程以及资源文件的加载 浏览器最大连接数 js资源 ...

  3. 套题T8&amp&semi;T9

    A - 8球胜负(eight) Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%lld & %llu Submi ...

  4. 【HDOJ】3234 Exclusive-OR

    并查集.对于对元素赋值操作,更改为I p n v.令val[n]=0(任何数与0异或仍为原值).考虑fa[x] = fx, fa[y] = fy.如果使得fa[fx] = fy, 那么val[fx] ...

  5. 关于Core Data的一些整理(五)

    关于Core Data的一些整理(五) 在Core Data中使用NSFetchedResultsController(以下简称VC)实现与TableView的交互,在实际中,使用VC有很多优点,其中 ...

  6. 【Spring源码深度解析系列 】Spring整体架构

    一.Spring的整体架构和模块 二.模块分类: 1.Core Container Core Container包含有Core .Beans.Context.和Expression  Language ...

  7. VMware workstation的基础使用

    1. VMware workstation虚拟化平台简介2. VMware workstation提供网络资源3. VMware workstation提供存储资源4. VMware workstat ...

  8. 使用genism训练词向量【转载】

    转自:https://blog.csdn.net/qq_16912257/article/details/79099581 https://blog.csdn.net/thriving_fcl/art ...

  9. SQLServer2016 AlwaysOn AG基于工作组的搭建笔记

    最近搭建了一套SQLServer2016 AlwaysOn AG. (后记:经实际测试,使用SQLServer2012 也同样可以在Winserver2016上搭建基于工作组的AlwaysOn AG, ...

  10. macbook下mysql安装

    1 原材料 1.1 mysql-5.7.22-macos10.13-x86_64.dmg 2 msql在macbook下的安装: 双击dmg进行解压, 再双击解压出来的pkg文件进行安装 3. Con ...