📜  C++ STL-multimap.equal_range()函数

📅  最后修改于: 2020-10-20 06:03:05             🧑  作者: Mango

C++ STL Multimap.equal_range()

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

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

句法

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

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

参数

k:要在Multimap容器中搜索的键。

返回值

该函数返回pair。对::第一在范围的下边界,具有与lower_bound(x)将返回的值相同的值,而对::第二是与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,因为它试图查找10(不是multimap mp的键)。

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