1. 添加元素insert

参数 返回值
m.insert(e); e为pair 返回一个pair, first指向e的一个迭代器,second 是bool值表示是否成功插入
m.emplace(args) args是参数列表
m.insert(beg,end); b e是迭代器 返回void
m.insert(il); 值的花括号列表 返回void
m.insert(iter,e); iter为辅助,e为pair 返回迭代器,指向m中具有特定键的元素
m.insert(iter,args); iter为辅助,args是参数列表 返回迭代器,指向m中具有特定键的元素

eg.

string word("dda");
//map<string, size_t> m = { { "2", 0 } };map<string, size_t> word_count;
word_count.insert({ word, 1 });//在参数列表中使用花括号初始化创建了一个pair。
word_count.insert(make_pair(word, 1));//也可在参数列表中调用make_pair构造pair。
word_count.insert(pair<string, size_t>(word, 1));//也可显示构造pair。
word_count.insert(map<string, size_t>::value_type(word, 1)); //先构造一个pair类型,再构造该类型的一个新对象(对象类型为pair)。map<string, size_t>::iterator a = word_count.begin();
map<string, size_t>::iterator b = ++a;cout << word_count.size() << endl;
word_count.insert(a, b);
word_count.insert(b, {"da", 0});
cout << word_count.size() << endl;

eg2.

向multiset或multimap添加元素
multimap<string, string> authors;
authors.insert({"author1", "book1"});
authors.insert({"author1", "book2"});

2. 遍历

std::map<int, int> word_count = { {0, 100},{ 1,200}, { 2,300 },{4,400} };
auto map_it = word_count.cbegin();
while (map_it != word_count.cend())
{...}

3. 删除

参数 返回值
m.erase(k) k为关键字 返回一个size_type,表示删除元素的数量
m.erase§ p是迭代器 p必须指向m中一个真实元素,返回指向p之后元素的迭代器(可以为end)
m.erase(beg,end) beg end是迭代器 返回end

4. 下标操作

c[k]
c.at(k)
区别是什么?

5. 访问元素

方法 返回值
c.find(k) 返回一个迭代器,指向第一个关键字为k的元素,若没有找到则返回end
c.count(k) 返回一个size_type,表示删除元素的数量
c.lower_bound(k) 返回一个迭代器,指向第一个关键字不小于k的元素
c.upper_bound(k) 返回一个迭代器,指向第一个关键字大于k的元素
c.equal_range(k) 返回一个迭代器pair,表示关键字等于k的元素范围。若k不存在,pair的两个成员均为end()

eg.

// map from author to title; there can be multiple titles per author
multimap<string, string> authors;// add data to authors
authors.insert({ "Alain de Botton", "On Love" });
authors.insert({ "Alain de Botton", "Status Anxiety" });
authors.insert({ "Alain de Botton", "Art of Travel" });
authors.insert({ "Alain de Botton", "Architecture of Happiness" });
/*
authors.insert(pair<string, string>("Alain de Botton", "On Love"));
authors.insert(pair<string, string>("Alain de Botton", "Status Anxiety"));
authors.insert(pair<string, string>("Alain de Botton", "Art of Travel"));
authors.insert(pair<string, string>("Alain de Botton", "Architecture of Happiness"));
*/
string search_item("Alain de Botton"); // author we'll look for
auto entries = authors.count(search_item); // number of elements
auto iter = authors.find(search_item); // first entry for this author
/*
// loop through the number of entries there are for this author
while (iter != authors.end()) {//while(entries) cout << iter->second << endl; // print each title++iter;     // advance to the next title--entries;  // keep track of how many we've printed
}
*/
// definitions of authors and search_item as above
// beg and end denote the range of elements for this author
for (auto beg = authors.lower_bound(search_item),end = authors.upper_bound(search_item);beg != end; ++beg)cout << beg->second << endl; // print each title// definitions of authors and search_item as above
// pos holds iterators that denote the range of elements for this key
for (auto pos = authors.equal_range(search_item);pos.first != pos.second; ++pos.first)cout << pos.first->second << endl; // print each title【引用】1. 代码
https://github.com/thefistlei/cplusprimer/blob/main/cprimer/cprimer/mapTest.h

C++ Primer 5th笔记(chap 11)关联容器操作相关推荐

  1. C++primer第十一章 关联容器 11.3关联容器操作 11.4 无序容器

    11.3关联容器操作 除了表9.2(第295页)中列出的类型,关联容器还定义了表11.3中列出的类型.这些类型表示容器关键字和值的类型. 对于set类型,key_type和value type是一样的 ...

  2. 关联容器----关联容器概述,关联容器操作,无序容器

    关联容器和顺序容器有着根本的不同:关联容器中的元素是按关键字来保存和访问的.与之相对,顺序容器中的元素是按它们在容器中的位置来顺序保存和访问的. 关联容器支持高效的关键字查找和访问.两个主要的关联容器 ...

  3. C++ Primer 5th笔记(chap 11)关联容器---无序容器

    无序关联容器 unordered associative container •unordered_map •unordered_set •unordered_multimap •unordered_ ...

  4. C++ Primer 5th笔记(chap 11)关联容器

    •map •multimap •set •multiset •set •unordered_map •unordered_set •unordered_multimap •unordered_mult ...

  5. 【C++ Primer 第11章】2. 关联容器操作

    练习答案 一.访问元素 关联容器额外类型别名  key_type 此容器类型的关键字类型 mapped_type 每个关键字关联的类型,只 适用于map mapped_type 对于set,与key_ ...

  6. C++ Primer 5th笔记(chap 14 重载运算和类型转换)算术和关系运算符

    运算符 双目算术运算符 + (加),-(减),*(乘),/(除),% (取模) 关系运算符 ==(等于),!= (不等于),< (小于),> (大于>,<=(小于等于),> ...

  7. C++ Primer 5th笔记(9)chapter9 顺序容器 构造和赋值

    1. 容器对象的构造和赋值 1.1 构造 C c; C c; //默认构造函数 C c1(c2) C c1=c2; C c{a,b,c,-} C c={a,b,c,-} C c(b,e)//eg. l ...

  8. C++ primer 5th笔记

    目录2022年11月25日 周五 阴 第一.二章:变量和基本类型 基本内置类型 unsigned float与double 'A'与"A" 布尔类型转换时 初始化与赋值 初始化 声 ...

  9. C++ Primer 5th笔记(10)chapter10 泛型算法 :泛型算法结构

    名称 定义 输入迭代器 只读,不写:单遍扫描,只能递增 输出迭代器 只写,不读:单遍扫描,只能递增 前向迭代器 可读写,多遍扫描,只能递增 双向迭代器 可读写,多遍扫描,可递增递减 随机访问迭代器 可 ...

最新文章

  1. linux命令在线查
  2. 如何在Excel中使用VB宏连接SAP系统
  3. [ solr入门 ] - 利用solrJ进行检索
  4. Java德才论宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之 小人。凡取人之术,苟不得圣人,君子而与之,与其得小人,不若
  5. 日常问题——使用Xshell 连接虚拟机报错 Disconnected from remote host
  6. R语言数组array函数
  7. Python 连续三年夺冠、PHP 受排挤,揭晓 IEEE Spectrum 2019 年度编程语言排行榜
  8. POJ NOI0101-09 字符菱形
  9. spring boot(三) 集成mybatis
  10. java+nanomsg(jnanomsg)
  11. python不定长参数_Python可变长参数
  12. ryuyan 方差分析_如何使用R语言做不同设计的方差分析(ANOVA)、简单效应检验、事后多重比较?...
  13. 云和恩墨数据库MogDB荣获2021年度IT168最佳创新产品奖
  14. spring使用ExcludeFilter和IncludeFilter
  15. 线段树 HDU 4046 panda
  16. 第十三届蓝桥杯 EDA 设计与开发科目 模拟试题(详细解读)
  17. 数据脱敏:保障数据安全的脱敏方案
  18. PAT.A1010 Radix
  19. 计算机视觉人脸检测与识别
  20. JAVA梅森旋转随机算法_伪随机数生成算法-梅森旋转(Mersenne Twister/MT)

热门文章

  1. 二阶系统响应指标图_15. 闭环系统的频域性能指标
  2. cpu工作原理flash动画_cpu的基本结构及其工作原理
  3. 支持app需要多大的服务器,上海app开发需要多大服务器空间?
  4. a extends b java_(class B extends A) 怎么解释?A是上一个类名
  5. 机房线路老化了会有怎么样的影响?又该如何处理?
  6. 深度linux win7分区,怎么安装Win7深度操作系统?
  7. CV:NVIDIA驱动程序安装图文教程(根据Anaconda的CUDA版本去安装对应匹配的NVIDIA)之详细攻略
  8. Python之schedule:schedule库的简介、安装、使用方法之详细攻略
  9. 成功解决\PyInstaller\compat.py, line 378  out = out.decode(encoding) UnicodeDecodeError: utf-8 codec c
  10. MAT之GA:GA优化BP神经网络的初始权值、阈值,从而增强BP神经网络的鲁棒性