在本文中,我们将讨论C ++ STL中map::lower_bound()函数的工作,语法和示例。
什么是C ++ STL中的映射?
映射是关联容器,它有助于按特定顺序存储由键值和映射值的组合形成的元素。在映射容器中,数据始终在内部借助其关联的键进行排序。映射容器中的值通过其唯一键访问。
什么是map::lower_bound()?
map::lower_bound()函数是C ++ STL中的内置函数,该函数在 header file. lower_bound() returns an iterator to the lower bound of the map container. This function returns an iterator which points to the first element which is considered to go before the key k.
语法Map_name.lower_bound(key& k);
参数
此函数仅接受1参数-k-我们要搜索的键。
返回值
该函数返回迭代器,该迭代器指向键“ k”的第一个元素,该元素被认为位于键k之前。
示例
输入项map newmap;
newmap[‘a’] = 1;
newmap[‘b’] = 2;
newmap[‘c’] = 3;
newmap.lower_bound(b);
输出结果a:1
示例#include
using namespace std;
int main() {
map TP_Map;
TP_Map.insert({5, 50});
TP_Map.insert({2, 30});
TP_Map.insert({1, 10});
TP_Map.insert({4, 70});
cout<
cout <
for (auto i = TP_Map.rbegin(); i!= TP_Map.rend(); i++) {
cout <first <second <
}
auto i = TP_Map.lower_bound(2);
cout <
cout <first <second <
auto i_1 = TP_Map.lower_bound(3);
cout <
cout <first <second <
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