C++ Multimap 中的 equal_range()函数

首页 / C++入门教程 / C++ Multimap 中的 equal_range()函数

C++ multimap equal_range()函数用于返回包含集合中等于x的所有关键元素的参数的边界。

如果x与集合中的任何键都不匹配,则返回值参数的长度为0,并且两个迭代器均指向大于x的最近值。否则,如果x大于集合中的所有元素,则它指向end。

equal_range - 语法

pair equal_range (const key_type& k) const;
pair             equal_range (const key_type& k);

该参数由两个迭代器定义。它返回参数的边界,该参数包括集合中所有具有等于k的键的元素。

equal_range - 参数

k :要在multimap集合中搜索的键。

equal_range - 返回值

该函数返回pair。其中pair::first位于参数的下边界,具有与lower_bound(x)返回的值相同的值,而pair::second与该上边界(x)将返回的值相同,它对应的参数的上限。

equal_range - 例子1

让我们看一个简单的例子:

无涯教程网

#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。

equal_range - 例子2

让我们看一个简单的例子:

无涯教程网

#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的键。

equal_range - 例子3

让我们看一个简单的例子:

无涯教程网

#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.

equal_range - 例子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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

软件测试52讲 -〔茹炳晟〕

Java并发编程实战 -〔王宝令〕

Selenium自动化测试实战 -〔郭宏志〕

用户体验设计实战课 -〔相辉〕

深度学习推荐系统实战 -〔王喆〕

技术面试官识人手册 -〔熊燚(四火)〕

结构会议力 -〔李忠秋〕

超级访谈:对话道哥 -〔吴翰清(道哥)〕

云原生基础架构实战课 -〔潘野〕

好记忆不如烂笔头。留下您的足迹吧 :)