C++ unordered_multimap::equal_range() 函式



C++ 的 std::unordered_multimap::equal_range() 函式用於返回一對迭代器,它們表示鍵與指定鍵等效的元素範圍。

眾所周知,equal_range() 函式返回一對迭代器,其中第一個迭代器指向範圍內與指定鍵等效的第一個元素,第二個迭代器指向該範圍的最後一個元素之後的位置。如果 unordered_multimap 中沒有與指定鍵等效的元素,則返回對中的兩個迭代器(下界和上界)都將等於容器末尾之後的位置,即 unordered_multimap.end()。

語法

以下是 std::unordered_multimap::equal() 函式的語法。

std::pair<iterator, iterator> equal_range(const key_type& key);

引數

  • k − 表示要比較或搜尋的鍵值。

返回值

此函式返回一對迭代器。

示例 1

在下面的示例中,我們演示了 unordered_multimap::equal_range() 函式的用法。

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_multimap<char, int> um = {
      {'a', 1},
      {'b', 2},
      {'c', 3},
      {'e', 4},
      {'a', 5},
      {'d', 6},
      {'e', 5}
   };
   auto ret = um.equal_range('e');
   cout << "Lower bound is " << ret.first->first 
       << " = "<< ret.first->second << endl;
   cout << "Upper bound is " << ret.second->first
       << " = " << ret.second->second << endl;
   return 0;
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果:

Lower bound is e = 5
Upper bound is c = 3

示例 2

考慮下面的示例,我們將訪問與範圍內鍵關聯的值。

#include <iostream>
#include <unordered_map>
using namespace std;
int main() {
   unordered_multimap<int, string> um = {
      {1, "one"},
      {2, "two"},
      {4, "four"},
      {3, "three"},
      {4, "four"},
      {5, "five"},
      {3, "three"},
   };
   auto range = um.equal_range(3);

   // Check if the key was found
   if (range.first != um.end()) {
      cout << "Key 3 found in the multimap!" << endl;
      // Access the values associated with keys in the range
      for (auto it = range.first; it != range.second; ++it) {
          cout << "The value associated with key " << it->first << " is: " << it->second << endl;
      }
   } else {
      cout << "Key 3 not found in the multimap!" << endl;
   }
   return 0;
}

輸出

如果我們執行上面的程式碼,它將生成以下輸出:

Key 3 found in the multimap!
The value associated with key 3 is: three
The value associated with key 3 is: third

示例 3

讓我們看一下下面的示例,我們將獲取重複鍵的值以及指定鍵的上界和下界。

#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
typedef unordered_multimap<string,string> stringmultimap;
int main () {
   stringmultimap umm = {
      {"orange","FL"},
      {"strawberry","LA"},
      {"strawberry","OK"},
      {"pumpkin","NH"}
   };
   cout << "Entries with strawberry: "<<endl;
   auto range = umm.equal_range("strawberry");
   for (auto it = range.first; it != range.second; ++it){
      cout<< it->first << " is: " << it->second<<endl;
   }
   cout << "Lower bound is " << range.first->first
      << " = "<< range.first->second << endl;
   cout << "Upper bound is " << range.second->first
      << " = " << range.second->second << endl;
   return 0;
}

輸出

以下是上述程式碼的輸出:

Entries with strawberry: 
strawberry is: OK
strawberry is: LA
Lower bound is strawberry = OK
Upper bound is pumpkin = NH

示例 4

以下示例將使用 multimap 並迭代指向範圍的鍵值對。

#include <iostream>
#include <unordered_map>
using namespace std;

int main() {
   unordered_multimap<int, int> umm;    
   umm.insert({ 1, 2 });
   umm.insert({ 1, 2 });
   umm.insert({ 2, 3 });
   umm.insert({ 3, 4 });
   umm.insert({ 2, 6 });
   auto range = umm.equal_range(1);
   cout << "Elements with Key 1: ";
   for (auto it = range.first; it != range.second; it++) {
      cout << it->second << " ";
   }
   
   cout << endl;
   range = umm.equal_range(2);
   cout << "Elements with Key 2: ";
   for (auto it = range.first; it != range.second; it++) {
      cout << it->second << " ";
   }
   return 0;
}

輸出

上述程式碼的輸出如下:

Elements with Key 1: 2 2 
Elements with Key 2: 6 3 
廣告