C++ multimap equal_range()函数

C++ multimap equal_range()函数

C++ multimap equal_range() 函数用于返回容器中与x相等的所有关键元素所在的范围的边界。

如果x与容器中的任何关键元素都不匹配,则返回值范围的长度为0,并且两个迭代器将指向x之后最近的较大值。否则,如果x大于容器中的所有元素,则它指向末尾。

语法

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

范围由两个迭代器定义。它返回一个范围的边界,该范围包括容器中所有与k相等的键的元素。

参数

k :要在多映射容器中搜索的键。

返回值

这个函数返回一对。其中pair :: first是与lower_bound(x)返回相同值的范围下边界,pair :: second是与upper_bound(x)返回相同值的范围上边界。

复杂度

对数级。

迭代器有效性

没有变化。

数据竞争

容器被访问(const和非const版本都不修改容器)。

没有访问到映射值:同时访问和修改元素是安全的。

异常安全性

如果抛出异常,容器不会发生变化。

示例1

让我们来看一个简单的示例:

#include 
#include 

using namespace std;

int main(void) {
   map 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。

示例2

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

#include 
#include 

using namespace std;

int main()
{

    // initialize container
    multimap mp;

    // insert elements in random order
    mp.insert({ 4, 30 });
    mp.insert({ 1, 40 });
    mp.insert({ 6, 60 });

    pair::iterator, multimap::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,因为它尝试查找不是multimap mp的键的10。

示例3

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

#include   
#include   

int main( )  
{  
   using namespace std;  

   typedef multimap  > IntMMap;  
   IntMMap m1;  
   multimap  :: const_iterator m1_RcIter;  
   typedef pair  Int_Pair;  

   m1.insert ( Int_Pair ( 1, 10 ) );  
   m1.insert ( Int_Pair ( 2, 20 ) );  
   m1.insert ( Int_Pair ( 3, 30 ) );  

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

示例4

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

#include 
#include 
#include 

int main()
{
  std::multimap m = {
    {"A", 3},
    {"B", 1},
    {"A", 4},
    {"D", 5}
  };

  using iterator = decltype(m)::iterator;
  std::pair ret = m.equal_range("B");

  for (iterator it = ret.first; it != ret.second; ++it) {
    std::cout << it->first << "," << it->second << std::endl;
  }
}

输出:

B, 1

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程