亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

請問 C++ map 的 lower_bound()函數怎么用?

請問 C++ map 的 lower_bound()函數怎么用?

回首憶惘然 2019-03-06 11:07:41
請問 C++ map 的 lower_bound()函數怎么用?
查看完整描述

2 回答

?
翻過高山走不出你

TA貢獻1875條經驗 獲得超3個贊

我們知道map容器是根據鍵值進行排序的

lower_bound(k)返回一個迭代器,指向鍵不小于k的第一個元素

upper_bound(k)返回一個迭代器,指向鍵大于k的第一個元素

這兩個函數常用于multimap容器,用來獲取某個鍵對應的所有元素

給你個程序:

12345678910111213141516171819202122232425#pragma warning (disable:4786)#include<iostream>#include<string>#include<map>using namespace std;int main(){    multimap<string,int> m;    m.insert(make_pair((string)"China",1));    m.insert(make_pair((string)"China",2));    m.insert(make_pair((string)"China",3));    m.insert(make_pair((string)"English",1));    m.insert(make_pair((string)"English",2));    multimap<string,int>::iterator it = m.begin();    while(it != m.end())    {        cout<<it->first<<" "<<it->second<<endl;        it++;    }    cout<<endl;    multimap<string,int>::iterator it1 = m.lower_bound("China"),it2 = m.upper_bound("China");    cout<<it1->first<<" "<<it1->second<<endl;    cout<<it2->first<<" "<<it2->second<<endl;    return 0;}

曉得了么,lower_bound(k)得到的迭代器,就是指向第一個鍵k對應的第一個元素

upper_bound(k)得到的迭代器,就是指向大于鍵k的第一個元素



查看完整回答
反對 回復 2019-03-20
?
FFIVE

TA貢獻1797條經驗 獲得超6個贊

1234iterator lower_bound(const key_type& _Keyval)    {   // find leftmost node not less than _Keyval in mutable tree    return (iterator(_Lbound(_Keyval), this));    }



查看完整回答
反對 回復 2019-03-20
  • 2 回答
  • 0 關注
  • 2449 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號