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

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

c++ 在multimap中查找关键字的程序举例

發(fā)布時(shí)間:2024/4/18 c/c++ 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++ 在multimap中查找关键字的程序举例 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在文件example.cc中有如下關(guān)于m1的定義:

//文件example.cc #include <string> #include <iostream> #include <map> using namespace std; multimap<string,int> m1{{"str1",1},{"str1",2},{"str1",3},{"str1",4},{"str1",5},{"str1",6}};

現(xiàn)在來(lái)查找m1中關(guān)鍵字等于"str1"的值分別是多少.

方法1:

#include <string> #include <iostream> #include <map> #include "example.cc" using namespace std; int main() { multimap<string,int>::iterator it = m1.find("str1"); int cnt = m1.count("str1"); int i = 0; for(i = 0;i != cnt; ++i){cout << it->second << endl;it ++;}

方法2:

#include <string> #include <iostream> #include <set> #include <map> #include "example.cc" using namespace std; int main() { multimap<string,int>::iterator it; for(it = m1.lower_bound("str1");it != m1.upper_bound("str1"); ++ it)cout << it->second << endl;

方法3:

#include <iostream> #include <string> #include <map> #include "example.cc" int main() { pair<multimap<string,int>::iterator,multimap<string,int>::iterator> p = m1.equal_range("str1"); for(auto it = p.first; it != p.second; ++it)cout << it->second << endl;return 0; } ~

說(shuō)明:(1)以上方法upper_bound和lower_bound函數(shù)不適用于無(wú)序容器。(打個(gè)比方,就好比是沒(méi)有順序的數(shù)列就沒(méi)有位置的區(qū)分了)

? ? ? ? ? ? ?(2)在有序關(guān)聯(lián)容器中,關(guān)鍵字相同的元素總是連續(xù)存儲(chǔ)的。

總結(jié)

以上是生活随笔為你收集整理的c++ 在multimap中查找关键字的程序举例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。