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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

STL中的multimap---顺便说说如何查找同一关键字对应的所有值

發(fā)布時間:2023/12/20 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 STL中的multimap---顺便说说如何查找同一关键字对应的所有值 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

轉(zhuǎn)載:http://blog.csdn.net/stpeace/article/details/44984639


我個人感覺哈, map的應(yīng)用場景比multimap更多, 不過, 我們還是來學一下multimap。 我們知道, multimap中, 一個關(guān)鍵字可能對應(yīng)多個不同的值, 怎么獲取呢?我們來看程序, 接招(介紹三種方法):


? ? ? 結(jié)果為:

[cpp]?view plaincopy
  • #pragma?warning(disable?:?4786)??
  • #include?<map>??
  • #include?<string>??
  • #include?<iostream>??
  • using?namespace?std;??
  • ??
  • int?main()??
  • {??
  • ????multimap<int,?string>?mp;??
  • ????mp.insert(pair<int,?string>(3,?"hehe"));??
  • ????mp.insert(pair<int,?string>(4,?"haha"));??
  • ????mp.insert(pair<int,?string>(2,?"error"));??
  • ????mp.insert(pair<int,?string>(3,?"good"));??
  • ????mp.insert(pair<int,?string>(3,?"ok"));??
  • ????mp.insert(pair<int,?string>(3,?"hehe"));??
  • ??
  • ????multimap<int,?string>::iterator?it;??
  • ????for(it?=?mp.begin();?it?!=?mp.end();?it++)??
  • ????{??
  • ????????cout?<<?it->first?<<?"--->";??
  • ????????cout?<<?it->second?<<?endl;??
  • ????}??
  • ??
  • ????//?方法一??
  • ????int?n?=?mp.count(3);?//?3的個數(shù)??
  • ????cout?<<?n?<<?endl;??
  • ??
  • ????int?i?=?0;??
  • ????it?=?mp.find(3);?//?第一個3的位置??
  • ????for(i?=?0;?i?<?n;?i++)??
  • ????{??
  • ????????cout?<<?it->first?<<?"--->";??
  • ????????cout?<<?it->second?<<?endl;??
  • ????????it++;?//?所有的3必然是相連的??
  • ????}??
  • ??
  • ????cout?<<?"---------------------------"?<<?endl;??
  • ??
  • ????//?方法二:??
  • ????for(it?=?mp.lower_bound(3);?it?!=?mp.upper_bound(3);?it++)??
  • ????{??
  • ????????cout?<<?it->first?<<?"--->";??
  • ????????cout?<<?it->second?<<?endl;??
  • ????}??
  • ??
  • ????cout?<<?"---------------------------"?<<?endl;??
  • ??
  • ????//?方法三:??
  • ????pair<multimap<int,?string>::iterator,?multimap<int,?string>::iterator?>?pos;??
  • ????for(pos?=?mp.equal_range(3);?pos.first?!=?pos.second;?pos.first++)??
  • ????{??
  • ????????cout?<<?pos.first->first?<<?"--->";??
  • ????????cout?<<?pos.first->second?<<?endl;??
  • ????}??
  • ??
  • ????return?0;??
  • }??
  • ? ? ? 結(jié)果為:

    2--->error
    3--->hehe
    3--->good
    3--->ok
    3--->hehe
    4--->haha
    4
    3--->hehe
    3--->good
    3--->ok
    3--->hehe
    ---------------------------
    3--->hehe
    3--->good
    3--->ok
    3--->hehe
    ---------------------------
    3--->hehe
    3--->good
    3--->ok
    3--->hehe
    Press any key to continue

    總結(jié)

    以上是生活随笔為你收集整理的STL中的multimap---顺便说说如何查找同一关键字对应的所有值的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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