std::flat_map<Key,T,Compare,KeyContainer,MappedContainer>::equal_range
來自 cppreference.com
std::pair<iterator, iterator> equal_range( const Key& key ); |
(1) | (C++23 起) |
std::pair<const_iterator, const_iterator> equal_range( const Key& key ) const; |
(2) | (C++23 起) |
template< class K > std::pair<iterator, iterator> equal_range( const K& x ); |
(3) | (C++23 起) |
template< class K > std::pair<const_iterator, const_iterator> equal_range( const K& x ) const; |
(4) | (C++23 起) |
返回包含容器中所有給定鍵的元素的範圍。該範圍由兩個迭代器定義,一個指向不小於 key 的第一個元素,另一個指向大於 key 的第一個元素。或者,第一個迭代器可以透過 lower_bound() 獲得,第二個迭代器可以透過 upper_bound() 獲得。
1,2) 將鍵與 key 進行比較。
3,4) 將鍵與值 x 進行比較。此過載僅在限定 ID Compare::is_transparent 有效並表示一種型別時才參與過載決議。它允許在不構造
Key
例項的情況下呼叫此函式。目錄 |
[編輯] 引數
鍵 | - | 用於比較元素的鍵值 |
x | - | 可與Key 比較的替代值 |
[編輯] 返回值
std::pair,包含一對定義所需範圍的迭代器:第一個指向不小於 key 的第一個元素,第二個指向大於 key 的第一個元素。
如果沒有不小於 key 的元素,則返回末尾(參見 end())迭代器作為第一個元素。類似地,如果沒有大於 key 的元素,則返回末尾迭代器作為第二個元素。
[編輯] 複雜度
容器大小的對數級別。
[編輯] 示例
執行此程式碼
#include <iostream> #include <flat_map> int main() { const std::flat_map<int, const char*> m { {0, "zero"}, {1, "one"}, {2, "two"} }; auto p = m.equal_range(1); for (auto& q = p.first; q != p.second; ++q) std::cout << "m[" << q->first << "] = " << q->second << '\n'; if (p.second == m.find(2)) std::cout << "end of equal_range (p.second) is one-past p.first\n"; else std::cout << "unexpected; p.second expected to be one-past p.first\n"; auto pp = m.equal_range(-1); if (pp.first == m.begin()) std::cout << "pp.first is iterator to first not-less than -1\n"; else std::cout << "unexpected pp.first\n"; if (pp.second == m.begin()) std::cout << "pp.second is iterator to first element greater-than -1\n"; else std::cout << "unexpected pp.second\n"; auto ppp = m.equal_range(3); if (ppp.first == m.end()) std::cout << "ppp.first is iterator to first not-less than 3\n"; else std::cout << "unexpected ppp.first\n"; if (ppp.second == m.end()) std::cout << "ppp.second is iterator to first element greater-than 3\n"; else std::cout << "unexpected ppp.second\n"; }
輸出
m[1] = one end of equal_range (p.second) is one-past p.first pp.first is iterator to first not-less than -1 pp.second is iterator to first element greater-than -1 ppp.first is iterator to first not-less than 3 ppp.second is iterator to first element greater-than 3
[編輯] 另請參閱
查詢具有特定鍵的元素 (公共成員函式) | |
檢查容器是否包含具有特定鍵的元素 (公共成員函式) | |
返回匹配特定鍵的元素數量 (公共成員函式) | |
返回指向第一個大於給定鍵的元素的迭代器 (公共成員函式) | |
返回指向第一個不小於給定鍵的元素的迭代器 (公共成員函式) | |
返回與特定鍵匹配的元素範圍 (函式模板) |