C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ multimap equal_range()

C++ multimap equal_range()

C++ multimap equal_range()函数用于返回包含容器中所有关键元素的范围边界等于x。
如果x与容器中的任何键都不匹配,则返回值范围为长度0,并且两个迭代器都指向大于x的最接近值。否则,如果x大于容器中的所有元素,则指向结尾。

语法

pair<const_iterator,const_iterator> equal_range (const key_type& k) const;
pair<iterator,iterator>             equal_range (const key_type& k);
范围由两个迭代器定义。它返回一个范围的边界,该范围包括容器中所有具有与k等效的键的元素。

参数

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

返回值

此函数返回对。其中对::第一位于范围的下边界,具有与lower_bound(x)返回的值相同的值,而对::第二与该上边界(x)将返回的值相同,它对应的范围的上限。

复杂度

大小为对数。

迭代器有效性

没有变化。

数据竞争

已访问容器(没有const非非const版本都不能修改容器)。
没有访问映射值: 同时访问和修改元素是安全的。

异常安全性

如果引发异常,则容器中没有任何更改。

示例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。

示例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的键)。

示例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.

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

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4