c++关联容器的容器操作(和顺序容器都支持的操作)详细解释,基础于c++primer 5th 表 9.2 (持续更新)
发布日期:2021-05-07 00:55:46 浏览次数:28 分类:精选文章

本文共 4401 字,大约阅读时间需要 14 分钟。

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

1.类型别名:

#include 
#include
#include
#include
#include
#include
using namespace std;int main(){string f = "file";ifstream in(f);map
words_count;string str;while(in >> str){words_count[str] ++;}//iteratormap
::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_iteratormap
::const_iterator it1 = words_count.cbegin();while(it1 !=words_count.end()){cout << it1->first << " occurs " << it1->second << (it1->second > 1 ? "times":"time")<
::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_typemap
::size_type s1;s1 = words_count.size();cout << "words_count 's size is:" << s1 << endl;//difference_typemap
::difference_type d;//d = words_count.end() - words_count.begin();//cout << d << endl;/*这里报错,gcc编译器支出,这里不支持-运算符,说明迭代器不能相减*///value_typefor(map
::value_type i : words_count)cout << i.first << endl;//reference or const_referencefor(map
::const_reference i : words_count) cout << i.first<< " occurs " << i.second << " times. " << endl;return 0;}

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

运行全部通过。

 

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

2.构造函数

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

总结:对于构造函数

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

3.赋值与swap

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

 关于swap例子:

#include 
#include
#include
#include
using namespace std;int main(){map
m1;map
m2;m1.swap(m2); return 0;}

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

#include 
#include
#include
#include
using namespace std;int main(){string str;map
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 4128102389400760775

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

#include 
#include
#include
#include
using namespace std;int main(){string str;map
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 a21281023894007607751not equal

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

#include 
#include
#include
#include
using namespace std;int main(){string str;map
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
::const_iterator it;for(it = words_cnt.cbegin(); it != words_cnt.end() ; ++ it) { cout << it->first << endl; cout << it->second << endl; } return 0;}

 

上一篇:ubuntu install fcitx
下一篇:关于map的一个习题,忽略大小写和标点符号单词的计数器

发表评论

最新留言

表示我来过!
[***.240.166.169]2025年03月22日 19时01分43秒