map::lower_bound(k)是C++ STL中的內置函數,該函數返回指向容器中鍵的迭代器,該迭代器等效於參數中傳遞的k。
用法:
map_name.lower_bound(key)
參數:該函數接受單個強製性參數鍵,該鍵指定要返回其lower_bound的元素。
返回值:該函數返回一個指向映射容器中鍵的迭代器,該迭代器等效於在參數中傳遞的k。如果在映射容器中不存在k,則該函數返回一個迭代器,該迭代器指向剛好大於k的緊鄰的下一個元素。如果在參數中傳遞的鍵超過了容器中的最大鍵,則迭代器返回指向映射中元素數量的鍵,即key和element = 0。
// C++ function for illustration
// map::lower_bound() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initialize container
map<int, int> mp;
// insert elements in random order
mp.insert({ 2, 30 });
mp.insert({ 1, 10 });
mp.insert({ 5, 50 });
mp.insert({ 4, 40 });
for (auto it = mp.begin(); it != mp.end(); it++) {
cout << (*it).first << " " << (*it).second << endl;
}
// when 2 is present
auto it = mp.lower_bound(2);
cout << "The lower bound of key 2 is ";
cout << (*it).first << " " << (*it).second << endl;
// when 3 is not present
// points to next greater after 3
it = mp.lower_bound(3);
cout << "The lower bound of key 3 is ";
cout << (*it).first << " " << (*it).second;
// when 6 exceeds
it = mp.lower_bound(6);
cout << "\nThe lower bound of key 6 is ";
cout << (*it).first << " " << (*it).second;
return 0;
}
相關用法
- C++ div()用法及代碼示例
- C++ fma()用法及代碼示例
- C++ log()用法及代碼示例
- C++ map key_comp()用法及代碼示例
- C++ map rend()用法及代碼示例
- C++ map rbegin()用法及代碼示例
- C++ map rbegin()用法及代碼示例
- C++ real()用法及代碼示例
- C++ imag()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 map lower_bound() function in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。