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

歡迎訪問 生活随笔!

生活随笔

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

c/c++

c++关联容器的成员函数find的一个例子

發布時間:2024/4/18 c/c++ 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++关联容器的成员函数find的一个例子 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?先來一個灰色難懂的部分鎮樓。

#include <string> #include <iostream> #include <list> #include <vector> #include <set> #include <map> #include <utility> #include <algorithm> using namespace std; int main() { set<int> iset = {0,1,2,3,4,5,6,7,8,9}; set<int>::iterator pos1 = iset.find(1); cout << *pos1 << endl; auto pos2 = iset.find(11); if(pos2 == iset.end())cout<< "yes" << endl; auto n = iset.count(1); cout << n << endl; auto n1 = iset.count(11); cout << n1 << endl;multimap<string,size_t> m1{{"abc",1},{"abc",2},{"abcd",3}}; auto pos3 = m1.find("abc"); if(pos3 == m1.end())cout << "end"<< endl; else cout << "not end" << endl; find(m1.begin(),m1.end(),pair<const string,size_t>{"abc",1}); find(m1.begin(),m1.end(),multimap<string,size_t>::value_type("abc",1)); find(m1.begin(),m1.end(),pair<const multimap<string,size_t>::key_type,size_t>("abc",1)); return 0; }

總結,關聯容器的find成員函數和algorithm中的find是完全兩回事,前一個是按照關鍵字查詢,另一個是直接查找元素。

關聯容器key_type是不帶const的。

例如,map<string,size_t>,那么key_type是string而不是const string.

第一部分:不允許重復關鍵字查找元素。

知識點1.下標和at操作只適合于非const的map和unordered_map,如下:

#include <string> #include <iostream> #include <set> #include <map> #include <utility> using namespace std; int main() { map<string,size_t> m{{"str1",1},{"str2",2},{"str3",3},{"str4",4}}; cout << m.at("str1") << endl; cout << m["str2"] << endl;return 0; }

知識點2:c.find(k),返回第一個指向關鍵字k的元素的迭代器,如果k不在其中,那么返回尾后迭代器,如下:

map<string,size_t>::iterator pos1 = m.find("str3"); cout << pos1->first << " "; cout << pos1->second << endl; set<int> sint {1,2,3,4,5,6,7,8,9}; auto pos2 = sint.find(4); cout << *pos2 << endl;

下面這句代碼就可以檢查"str1"是否在m中,如果不在,打印一條消息:?

if(m.find("str1") == m.end())cout << "str1" << " is not in the map.\n" << endl;

知識點3.? c.count(k),返回關鍵字是k的元素的數量,對于不允許重復關鍵字的容器,返回值始終是1或者0.

auto pos3 = sint.count(2); cout << pos3 << endl; auto pos4 = m.count("str2"); cout << pos4 << endl;

count函數始終返回關鍵字的個數。

知識點4. c.lower_bound(k),c.upper_bound(k)

c.lower_bound(k)返回第一個不小于k的元素的迭代器,c.upper_bound(k)返回第一個關鍵字大于k的元素。

#include <string> #include <iostream> #include <set> #include <map> #include <utility> using namespace std; int main() { multimap<string,size_t> m{{"str1",1},{"str2",2},{"str3",3},{"str4",4},{"str2",4},{"str2",5}};//multimap對象不能使用下標和at函數 //cout << m.at("str1") << endl; //cout << m["str2"] << endl;auto pos5 = m.lower_bound("str2"); auto pos6 = m.upper_bound("str2"); for(auto i = pos5;i != pos6;++i)cout << i->first << " "<< i->second << endl;return 0;}

執行結果:?

str2 2 str2 4 str2 5

其實,lower_bound和upper_bound主要是用來找幾個相等元素的迭代器的。如果沒有相等元素,它們2個返回相等的,元素k的安全的插入位置(不破壞原有順序)

第二部分:允許重復關鍵字查找元素

m是作者到著作的一個映射,而且是multimap,下面的方法可以查出所有某個作者的著作。

#include <string> #include <iostream> #include <set> #include <map> using namespace std; int main() { multimap<string,string> m{{"white","the game theory"},{"white","the game2 theory"},{"robert","the c program language"},{"william","the python"},{"joe","the c++ program language"},{"white","the java language"},{"white","thie html"}};int num = m.count("white"); multimap<string,string>::iterator it; it = m.find("white"); while(num){cout << it -> second << endl;++ it;-- num;}return 0;}

注意:c.find(k),作用是查找c內第一個關鍵字等于k的元素??梢酝ㄟ^得到的第一個迭代器逐漸 ++,得到后面等于k的元素的所有迭代器。

結論:對于有序的關聯容器,所有的元素都是按照關鍵字的順序存儲的。

上述問題也可以利用lower_bound()和upper_bound()來解決,如下:

#include <string> #include <iostream> #include <set> #include <map> using namespace std; int main() { multimap<string,string> m{{"white","the game theory"},{"white","the game2 theory"},{"robert","the c program language"},{"william","the python"},{"joe","the c++ program language"},{"white","the java language"},{"white","thie html"}};for(multimap<string,string>::iterator it = m.lower_bound("white");it != m.upper_bound("white");++ it){cout << it->second << endl;}return 0;} the game theory the game2 theory the java language thie html

說明:c.lower_bound(k),返回第一個大于等于k的元素的迭代器,也就是說,如果有相等元素,那就是返回第一個等于k的迭代器。c.upper_bound(k)是返回第一個大于k的迭代器。

? (c.lower_bound(k),c.upper_bound(k))正好是一個前閉后開的等于元素k的迭代器范圍。

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的c++关联容器的成员函数find的一个例子的全部內容,希望文章能夠幫你解決所遇到的問題。

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