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