关联容器和顺序容器都是容器,所以都支持容器操作。就是表格9.2所示的操作。以下以map为例测试表格中的操作是否满足关联容器。

1.类型别名:

#include <iostream>
#include <string>
#include <map>
#include <set>
#include <iterator>
#include <fstream>
using namespace std;int main()
{
string f = "file";
ifstream in(f);
map<string,unsigned> words_count;
string str;
while(in >> str)
{
words_count[str] ++;
}//iterator
map<string,unsigned>::iterator it;
for(it = words_count.begin(); it != words_count.end();++it)
{
cout << it->first << " occurs " << it->second << (it->second > 1?"times":"time") << endl;
}
//const_iterator
map<string,unsigned>::const_iterator it1 = words_count.cbegin();
while(it1 !=words_count.end())
{
cout << it1->first << " occurs " << it1->second << (it1->second > 1 ? "times":"time")<<endl;
++it1;
}
//reverse_iterator
map<string,unsigned>::const_reverse_iterator it3;
for(it3 = words_count.crbegin();it3 != words_count.crend();++it3)
cout << it3->first << " occurs " << it3->second << (it3->second > 1?"times":"time")<< endl;//size_type
map<string,unsigned>::size_type s1;
s1 = words_count.size();
cout << "words_count 's size is:" << s1 << endl;//difference_type
map<string,unsigned>::difference_type d;
//d = words_count.end() - words_count.begin();
//cout << d << endl;
/*这里报错,gcc编译器支出,这里不支持-运算符,说明迭代器不能相减*///value_type
for(map<string,unsigned>::value_type i : words_count)
cout << i.first << endl;//reference or const_reference
for(map<string,unsigned>::const_reference i : words_count)cout << i.first<< " occurs " << i.second << " times. " << endl;return 0;
}

文件file是一个存放数据的文件.

运行全部通过。

总结,g++编译器不支持关联容器的迭代器的 -  减法运算符。其余表格9.2里面的map都支持。set情况大家自己可以实验。

2.构造函数

#include <iostream>
#include <string>
#include <map>
#include <set>
#include <fstream>
using namespace std;
int main()
{
string f= "file";
ifstream in(f);
map<string,unsigned> words_count;
set<string> words;
string str;while(in >> str)
{words_count[str] ++;
}
in.close();
in.open(f);//下面测试构造函数set先
set<string> s1{"str1","str2","str3","str4","str5","str6","str7"};
set<string> s2;
set<string> s3(s1);
set<string> s4(s1.begin(),s1.end());//再看看map
map<string,int> m1;
while(in >>str)
{
m1[str] ++;
}
map<string,int> m2(m1);
map<string,int> m3(m1.begin(),m1.end());
map<string,int> m4;return 0;
}

总结:对于构造函数

C c;   C  c1(c2);      C   c(c1.begin(),c1.end());   C  c{p1,p2,p3...};都成立,就是map的列表初始化还没学到。这里没法实验。

3.赋值与swap

#include <iostream>
#include <string>
#include <map>
#include <set>
#include <fstream>
using namespace std;
int main()
{
string f= "file";
ifstream in(f);
map<string,unsigned> words_count;
set<string> words;
string str;
while(in >> str)
{words_count[str] ++;
}//map ' s assignment// from ==
map<string,unsigned> m1;
m1 = words_count;//set 's assignment// from {}
set<string> s1 = {"ab","ac","ad","efc"};// from =
set<string> s2 = s1;
s2 = {"robert","liming","Wiliam"};
for(set<string>::const_reference i : s2)cout << i << endl;return 0;
}

关于swap例子:

#include <string>
#include <iostream>
#include <map>
#include <set>
using namespace std;
int main()
{
map<string,int> m1;
map<string,int> m2;
m1.swap(m2);return 0;
}

4.c.size(),c.max_size(),c.empty(),c.clear()都满足

#include <string>
#include <iostream>
#include <set>
#include <map>
using namespace std;
int main()
{
string str;
map<string,int> words_cnt;
if(words_cnt.empty())cout << "words_cnt is empty!!!" << endl;
while(cin >> str)
{words_cnt[str] ++;
}
cout << words_cnt.size() << endl;
cout << words_cnt.max_size() << endl;
return 0;
}
~    

运行结果如下:

words_cnt is empty!!!
asd fasd fasdf asdf asdf
4
128102389400760775

5.关系运算符==、!=、<=、>=、>、<

#include <string>
#include <iostream>
#include <set>
#include <map>
using namespace std;
int main()
{
string str;
map<string,int> words_cnt,m2;
if(words_cnt.empty())cout << "words_cnt is empty!!!" << endl;
while(cin >> str)
{words_cnt[str] ++;
}
cout << words_cnt.size() << endl;
cout << words_cnt.max_size() << endl;cout << (words_cnt > m2) << endl;
if(words_cnt == m2)cout << "equal" << endl;
else cout << "not equal" << endl;
return 0;
}

运行如下:

words_cnt is empty!!!
asdf
asdf asdf a
2
128102389400760775
1
not equal

6.迭代器c.cbegin()和c.cend()

#include <string>
#include <iostream>
#include <set>
#include <map>
using namespace std;
int main()
{
string str;
map<string,int> words_cnt,m2;
if(words_cnt.empty())cout << "words_cnt is empty!!!" << endl;
while(cin >> str)
{words_cnt[str] ++;
}
cout << words_cnt.size() << endl;
cout << words_cnt.max_size() << endl;cout << (words_cnt > m2) << endl;
if(words_cnt == m2)cout << "equal" << endl;
else cout << "not equal" << endl;cout << "print :" << endl;
map<string,int>::const_iterator it;
for(it = words_cnt.cbegin(); it != words_cnt.end() ; ++ it){cout << it->first << endl;cout << it->second << endl;}return 0;
}

c++关联容器的容器操作(和顺序容器都支持的操作)详细解释,基础于c++primer 5th 表 9.2 (持续更新)相关推荐

  1. STL 容器和迭代器连载6_顺序容器的操作3

    2019独角兽企业重金招聘Python工程师标准>>> /*- ========================================================== ...

  2. 【WFS】WFS 的所有版本都支持以下操作

    WFS 的当前版本是2.0.0.GeoServer 支持版本 2.0.0.1.1.0 和 1.0.0.尽管版本之间存在一些重要差异,但请求语法通常保持不变, WFS 的所有版本都支持以下操作: Get ...

  3. 我对STM32所用位带操作宏的超详细剖析、优势分析及应用推广探索研究(持续更新,欢迎讨论交流)

    在原子例程的sys.h中,使用宏定义建立了位带操作的基础, 使得操作IO端口可以像51一样实现位操作. 其实深入了解了位带操作的原理,几乎就可以实现对STM32所有外设寄存器的访问, 极端情况下,什么 ...

  4. C++primer第九章 顺序容器 9.3 顺序容器操作

    9.3顺序容器操作 顺序容器和关联容器的不同之处在于两者组织元素的方式.这些不同之处直接关系到了元素如何存储.访问.添加以及删除.上一节介绍了所有容器都支持的操作(罗列于表9.2(第295页)).本章 ...

  5. 《C++Primer》第九章-顺序容器-学习笔记(1)-顺序容器定义与操作

    <C++Primer>第九章-顺序容器-学习笔记(1) 文章目录 <C++Primer>第九章-顺序容器-学习笔记(1) 摘要 顺序容器的定义 容器元素的初始化 将一个容器初始 ...

  6. C++ Primer 第9章 顺序容器 第一次学习笔记

    1. 顺序容器概述 #include <vector> //可变大小数组.支持快速随机访问.在尾部之外的位置插入或删除元素可能很慢 #include <deque> //双端队 ...

  7. 顺序容器----顺序容器概述,容器库概览

    一.顺序容器概述 一个容器就是一些特定类型对象的集合.顺序容器为程序员提供了控制元素存储和访问顺序的能力.这种顺序不依赖于元素的值,而是与元素加入容器时的位置相对应. 顺序容器类型: 容器类型 说明 ...

  8. 顺序容器(vector、list、string、deque、forward_list)及迭代器、容器适配器

    文章目录 概述 所有容器都支持的操作 迭代器 迭代器支持的操作 迭代器支持的算术运算 容器类型 size_type iterator 和 const_iterator 容器定义和初始化 拷贝初始化 顺 ...

  9. 《C++ Primer 5th》笔记(9 / 19):顺序容器

    文章目录 顺序容器概述 确定使用哪种顺序容器 容器库概览 迭代器 迭代器范围 使用左闭合范围蕴含的编程假定 容器类型成员 begin和end成员 容器定义和初始化 将一个容器初始化为另一个容器的拷贝 ...

最新文章

  1. Yii CGridView 基本使用(三)关联表相关字段搜索
  2. OpenCV霍夫变换的演示代码(附完整代码)
  3. import package的问题
  4. python3生成二维码实例fromm_Python使用mqtt极简例子
  5. python导入模块以及类_python模块的导入以及模块简介
  6. oracle 10g 扩表空间,Oracle 10g 表空间管理(一)
  7. mysql too many connection解决方法
  8. 华为GPON设备ONU常见告警及处理
  9. 焊接工时简便计算工具_焊接工时计算表
  10. UML建模:基于智慧校园的二手交易平台
  11. linux开发板挂载
  12. php去除微信特殊符号,PHP方法处理微信昵称特殊符号过滤
  13. STC12C5A60S2 双串口
  14. jquery validate插件onfocosout/onkeyup出错的解决方法
  15. 关于JPsh极光推送的基本用法和通知介绍
  16. 你们还不了解YUM的使用?那就看看这篇文章把~
  17. Prior-Induced Information Alignment for Image Matting
  18. iOS App 签名的原理 App 重签名(三)
  19. 基于layui的后台管理模板
  20. 移动app测试经验分享

热门文章

  1. 数学、物理算法ActionScript实现(2)
  2. [android] 百度地图开发 (二).定位城市位置和城市POI搜索
  3. iOS之深入解析内存管理NSTimer的强引用问题
  4. Swift之实现表格UITableView数据首字母顺序排列展示并添加“索引”快速定位查找功能
  5. 51. N-Queens N 皇后
  6. Jenkins 创建一个freestyle的Job
  7. BEGIN-2 序列求和
  8. 【嵌入式】C语言高级编程-内建函数(11)
  9. 【Linux网络编程】套接字的介绍
  10. android 圆形选中,RoundChoiceView