C++ multimap equal_range()函数用于返回包含集合中等于x的所有关键元素的参数的边界。
如果x与集合中的任何键都不匹配,则返回值参数的长度为0,并且两个迭代器均指向大于x的最近值。否则,如果x大于集合中的所有元素,则它指向end。
pairequal_range (const key_type& k) const; pair equal_range (const key_type& k);
该参数由两个迭代器定义。它返回参数的边界,该参数包括集合中所有具有等于k的键的元素。
k :要在multimap集合中搜索的键。
该函数返回pair。其中pair::first位于参数的下边界,具有与lower_bound(x)返回的值相同的值,而pair::second与该上边界(x)将返回的值相同,它对应的参数的上限。
让我们看一个简单的例子:
#include <iostream> #include <map> using namespace std; int main(void) { map<char, int> m = { {'a', 1}, {'b', 2}, {'c', 3}, {'c', 4}, {'e', 5}, }; auto ret = m.equal_range('b'); cout << "Lower bound of b is: " << ret.first->first << " = " << ret.first->second << endl; cout << "Upper bound of b is: " << ret.second->first << " = " << ret.second->second << endl; return 0; }
输出:
Lower bound of b is: b = 2 Upper bound of b is: c = 3
在上面的示例中,b的下限为b,b的上限为c。
让我们看一个简单的例子:
#include <iostream> #include <multimap> using namespace std; int main() { //initialize container multimap<int, int> mp; //insert elements in random order mp.insert({ 4, 30 }); mp.insert({ 1, 40 }); mp.insert({ 6, 60 }); pair<multimap<int, int>::iterator, multimap<int, int>::iterator> it; //iterator of pairs it = mp.equal_range(10); cout << "The lower bound is " << it.first->first << ":" << it.first->second; cout << "\nThe upper bound is " << it.second->first << ":" << it.second->second; return 0; }
输出:
The lower bound is 3:0 The upper bound is 3:0
在上面的示例中,equal_range()函数返回0,因为它试图查找10,这不是multimap mp的键。
让我们看一个简单的例子:
#include <map> #include <iostream> int main( ) { using namespace std; typedef multimap <int, int, less<int> > IntMMap; IntMMap m1; multimap <int, int> ::const_iterator m1_RcIter; typedef pair <int, int> Int_Pair; m1.insert ( Int_Pair ( 1, 10 ) ); m1.insert ( Int_Pair ( 2, 20 ) ); m1.insert ( Int_Pair ( 3, 30 ) ); pair <IntMMap::const_iterator, IntMMap::const_iterator> p1, p2; p1 = m1.equal_range( 2 ); cout << "The lower bound of the element with " << "a key of 2 in the multimap m1 is: " << p1.first -> second << "." << endl; cout << "The upper bound of the element with " << "a key of 2 in the multimap m1 is: " << p1.second -> second << "." << endl; //Compare the upper_bound called directly m1_RcIter = m1.upper_bound( 2 ); cout << "A direct call of upper_bound( 2 ) gives " << m1_RcIter -> second << "," << endl << " matching the 2nd element of the pair" << " returned by equal_range( 2 )." << endl; p2 = m1.equal_range( 4 ); //If no match is found for the key, //both elements of the pair return end( ) if ( ( p2.first == m1.end( ) ) && ( p2.second == m1.end( ) ) ) cout << "The multimap m1 doesn't have an element " << "with a key less than 4." << endl; else cout << "The element of multimap m1 with a key >= 40 is: " << p1.first -> first << "." << endl; }
输出:
The lower bound of the element with a key of 2 in the multimap m1 is: 20. The upper bound of the element with a key of 2 in the multimap m1 is: 30. A direct call of upper_bound( 2 ) gives 30, matching the 2nd element of the pair returned by equal_range( 2 ). The multimap m1 doesn't have an element with a key less than 4.
让我们看一个简单的例子:
#include <iostream> #include <string> #include <map> int main() { std::multimap<std::string, int> m = { {"A", 3}, {"B", 1}, {"A", 4}, {"D", 5} }; using iterator = decltype(m)::iterator; std::pair<iterator, iterator> ret = m.equal_range("B"); for (iterator it = ret.first; it != ret.second; ++it) { std::cout << it->first << "," << it->second << std::endl; } }
输出:
B, 1
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)