日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

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

發布時間:2024/4/18 c/c++ 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++关联容器的容器操作(和顺序容器都支持的操作)详细解释,基础于c++primer 5th 表 9.2 (持续更新) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

關聯容器和順序容器都是容器,所以都支持容器操作。就是表格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 (持续更新)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。