multiset::upper_bound()是C++ STL中的內(nèi)置函數(shù),該函數(shù)返回一個(gè)迭代器,該迭代器指向剛好大于key的下一個(gè)元素。如果參數(shù)中傳入的鍵超過了容器中的大鍵,則返回的迭代器將指向一個(gè)元素,確切的說解引用后是一個(gè)index或者pos值,該數(shù)值對(duì)應(yīng)容器中最后一個(gè)元素之后的位置。
創(chuàng)新互聯(lián)客戶idc服務(wù)中心,提供服務(wù)器托管、成都服務(wù)器、成都主機(jī)托管、成都雙線服務(wù)器等業(yè)務(wù)的一站式服務(wù)。通過各地的服務(wù)中心,我們向成都用戶提供優(yōu)質(zhì)廉價(jià)的產(chǎn)品以及開放、透明、穩(wěn)定、高性價(jià)比的服務(wù),資深網(wǎng)絡(luò)工程師在機(jī)房提供7*24小時(shí)標(biāo)準(zhǔn)級(jí)技術(shù)保障。* A built-in function in C++ STL that returns an iterator pointing to the immediate next element which is just greater than k.
** If the key passed in the parameter exceeds the maximum key in the container, then the iterator returned points an element which points to the position after the last element in the container
multiset_name.upper_bound(key)
#include#includeusing namespace std;
int main()
{
multisets;
// Function to insert elements
// in the multiset container
s.insert(1);
s.insert(3);
s.insert(3);
s.insert(5);
s.insert(4);
s.insert(8);
cout<< "The multiset elements are:";
for (auto it = s.begin(); it != s.end(); it++)
cout<< *it<< " ";
// when 3 is present
auto it = s.upper_bound(3);
cout<< "\nThe upper bound of key 3 is ";
cout<< (*it)<< endl;
// when 2 is not present
// points to next greater after 2
it = s.upper_bound(2);
cout<< "The upper bound of key 2 is ";
cout<< (*it)<< endl;
// when 8 equal the max element in multiset
it = s.upper_bound(8);
cout<< "The upper bound of key 8 is ";
cout<< (*it)<< endl; // result is equivalent to exceeds the max element in multiset.
// when 10 exceeds the max element in multiset
it = s.upper_bound(10);
cout<< "The upper bound of key 10 is ";
cout<< (*it)<< endl; // get a index which is just greater than max element in multiset
// the max element in multiset is 8, which index is 5, so we get a new position is 6.
return 0;
}
Output:
The multiset elements are:1 3 3 4 5 8
The upper bound of key 3 is 4
The upper bound of key 2 is 3
The upper bound of key 8 is 6
The upper bound of key 10 is 6
multiset::lower_bound()是C++ STL中的內(nèi)置函數(shù),該函數(shù)返回一個(gè)迭代器,該迭代器指向剛好等于key的一個(gè)元素。如果參數(shù)中傳遞的鍵不在容器中,則返回的迭代器將指向剛好大于key的那個(gè)元素,如果傳入的key超過了容器中的大鍵,則返回的迭代器將指向一個(gè)元素確切的說解引用后是一個(gè)index或者pos值,該數(shù)值對(duì)應(yīng)容器中最前面一個(gè)元素之后的位置。
* A built-in function in C++ STL which returns an iterator pointing to the first element in the container which is equivalent to k passed in the parameter.
** In case k is not present in the set container, the function returns an iterator pointing to the immediate next element which is just greater than k.
*** If the key passed in the parameter exceeds the maximum value in the container, then the iterator returned point the number of elements in the container
multiset_name.lower_bound(key)
#include#includeusing namespace std;
int main()
{
multisets;
// Function to insert elements
// in the multiset container
s.insert(2);
s.insert(3);
s.insert(3);
s.insert(2);
s.insert(6);
cout<< "The multiset elements are: ";
for (auto it = s.begin(); it != s.end(); it++)
cout<< *it<< " "; // order by key automaticly, from little to bigger
// when 3 is present
auto it = s.lower_bound(3);
cout<< "\nThe lower bound of key 3 is ";
cout<< (*it)<< endl;
// when 0 is not present
// points to next element just greater after 0
it = s.lower_bound(0);
cout<< "The lower bound of key 0 is ";
cout<< (*it)<< endl;
// when 5 is not present
// points to next element just greater after 5
it = s.lower_bound(5);
cout<< "The lower bound of key 5 is ";
cout<< (*it)<< endl;
// when 8 exceeds the max element in multiset
it = s.lower_bound(8);
cout<< "The lower bound of key 8 is ";
cout<< (*it)<< endl; // get a index or pos which is just greater than the max element in multiset
// the max element in multisetis 6, which pos is 4, so we get a new position is 5.
return 0;
}
Output:
The multiset elements are: 2 2 3 3 6
The lower bound of key 3 is 3
The lower bound of key 0 is 2
The lower bound of key 5 is 6
The lower bound of key 8 is 5
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧