C++ STL 中的 multimap lower_bound() 函数
在本文中,我们将讨论 C++ STL 中 multimap::lower_bound() 函数的工作原理、语法和示例。
什么是 C++ STL 中的 Multimap?
Multimap 是关联容器,类似于 map 容器。它还可以方便地以特定顺序存储由键值和映射值组合而成的元素。在 multimap 容器中,可以有多个元素与同一个键相关联。数据在内部始终借助其关联的键进行排序。
什么是 multimap::lower_bound()?
multimap::lower_bound() 函数是 C++ STL 中的内置函数,它在 <map> 头文件中定义。lower_bound() 返回指向 multimap 容器下界的迭代器。此函数返回一个迭代器,该迭代器指向被认为在键 k 之前的第一个元素。
语法
multi.lower_bound(key& k);
参数
此函数仅接受 1 个参数:
k - 我们要搜索的键。
返回值
此函数返回指向键“k”的第一个元素的迭代器,该元素被认为在键 k 之前。
输入
multimap<char, int> newmap; multimap<char, int > newmap; newmap.insert(make_pair(‘a’, 1)); newmap.insert(make_pair(‘b’, 2)); newmap.insert(make_pair(‘c’, 3)); newmap.lower_bound(b);
输出
a:1
示例
#include <bits/stdc++.h>
using namespace std;
int main(){
//creating a multimap
multimap<int, int> mul;
mul.insert({ 2, 10 });
mul.insert({ 1, 20 });
mul.insert({ 1, 30 });
mul.insert({ 3, 40 });
mul.insert({ 3, 50 });
mul.insert({ 4, 60 });
// lower bound of 1
auto i = mul.lower_bound(1);
cout << "Lower bound of key 1 is: ";
cout << (*i).first << " " << (*i).second << endl;
// Lower bound of 2
i = mul.lower_bound(2);
cout << "Lower bound of key 2 is: ";
cout << (*i).first <<" "<<(*i).second << endl;
// Lower bound of 3
i = mul.lower_bound(3);
cout << "Lower bound of key 3 is: ";
cout << (*i).first << " " << (*i).second << endl;
return 0;
}输出
如果我们运行上述代码,它将生成以下输出:
Lower bound of key 1 is: 1 20 Lower bound of key 2 is: 2 10 Lower bound of key 3 is: 3 40
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP