C++ STL 中的 upper_bound() 函式對映
在本文中,我們將討論 C++ STL 中 map::upper_bound() 函式的工作原理、語法和示例。
什麼是 C++ STL 中的 Map?
Map 是關聯容器,有助於儲存由鍵值和對映值組成的元素,並按特定順序排列。在 map 容器中,資料始終藉助其關聯鍵以內部方式進行排序。透過其唯一鍵訪問 map 容器中的值。
什麼是 map::upper_bound()?
map::upper_bound() 函式是 C++ STL 中的內建函式,在中定義
語法
Map_name.upper_bound(key& k);
引數
此函式僅接受 1 個引數 -
- k - 我們想要搜尋的鍵。
返回值
此函式返回指向鍵‘k’的下一元素的迭代器,該元素被視為在鍵 k 之後。
示例
輸入
map<char, int> newmap; newmap[‘a’] = 1; newmap[‘b’] = 2; newmap[‘c’] = 3; newmap.upper_bound(b);
輸出
c:3
示例
#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.upper_bound(2);
cout << "The upper bound of key 2 is ";
cout << i->first << " " << i->second << endl;
auto i_1 = TP_Map.upper_bound(3);
cout << "The upper 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 upper bound of key 2 is 4 :70 The upper bound of key 3 is 4 :70
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
安卓
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP