map lower_bound() 函式在 C++ STL
本文將討論 C++ STL 中 map::lower_bound() 函式的工作原理、句法和示例。
C++ STL 中的對映是什麼?
對映是有序關聯容器,它有助於以特定順序儲存由鍵值和對映值組合形成的元素。在對映容器中,資料的內部始終透過其關聯的鍵進行排序。對映容器中的值透過其唯一鍵進行訪問。
什麼是 map::lower_bound()?
map::lower_bound() 函式是 C++ STL 中的內建函式,它在
語法
Map_name.lower_bound(key& k);
引數
此函式僅接受 1 個引數 −
- k − 我們想要搜尋的鍵。
返回值
此函式返回一個迭代器,指向鍵“k”的第一個元素,該元素應視為在鍵 k 之前。
示例
輸入
map<char, int> newmap; newmap[‘a’] = 1; newmap[‘b’] = 2; newmap[‘c’] = 3; newmap.lower_bound(b);
輸出
a:1
示例
#include <bits/stdc++.h>
using namespace std;
int main() {
map<int, int> TP_Map;
TP_Map.insert({5, 50});
TP_Map.insert({2, 30});
TP_Map.insert({1, 10});
TP_Map.insert({4, 70});
cout<<"\nTP Map is : \n";
cout << "MAP_KEY\tMAP_ELEMENT\n";
for (auto i = TP_Map.rbegin(); i!= TP_Map.rend(); i++) {
cout << i->first << "\t" << i->second << endl;
}
auto i = TP_Map.lower_bound(2);
cout << "The lower bound of key 2 is ";
cout << i->first << ": " << i->second << endl;
auto i_1 = TP_Map.lower_bound(3);
cout << "The lower bound of key 3 is ";
cout << i_1->first << " :" << i_1->second << endl;
return 0;
}輸出
TP Map is: MAP_KEY MAP_ELEMENT 5 50 4 70 2 30 1 10 The lower bound of key 2 is 2 :30 The lower bound of key 3 is 4 :70
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP