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. [bat批处理文件] 压缩备份
  2. SQL Server on Linux的文件和目录结构
  3. Python基础44(PyMySQL模块)
  4. 那些汽车搭载鸿蒙,首台搭载鸿蒙OS的汽车要来了?
  5. java Servlet Session
  6. php通配符是什么,通配符*和?有什么区别
  7. sql server 性能_SQL Server预读机制; 概念和性能提升
  8. java设置子线程优先级_Java 实例 - 线程优先级设置
  9. 在linux桌面上显示图标
  10. 【转】通过CountDownLatch提升请求处理速度
  11. python提前退出内层循环,python with提前退出遇到的坑与解决方案
  12. 关于水题POJ2159
  13. linux文件重定向用什么命令实现,Linux基础:文件描述符与重定向
  14. java数组函数_Java数组
  15. perl语言入门学习
  16. SOEM控制伺服电机
  17. 小程序跳转样式布局错乱_微信小程序页面布局问题
  18. cmd命令查询电脑序列号_什么命令可以查电脑型号、序列号
  19. 软件各种版本的含义!例如RC,M,GA等等
  20. 2018的锅让2019来悲

热门文章

  1. IDEA_Spring Data JPA有关报错Cannot resolve table 'XXX'
  2. li变成行内块级元素之后,每一个li之间会有间隙,这是为什么?
  3. 第十届蓝桥杯java B组—试题I 后缀表达式
  4. python当输入负数时结束_如何在lis中输入负数
  5. python多级字典嵌套_python – 如何拆分字符串并形成多级嵌套字典?
  6. 致:测试工程师们!超2000种连接件,下载96页完整选型目录!
  7. 想避免宕机,数据中心运营商还要不断演练实践
  8. echarts仪表盘option_echarts仪表盘完整代码
  9. MAT之GA:利用GA对一元函数进行优化过程,求x∈(0,10)中y的最大值
  10. 成功解决\h5py\__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float