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