当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ unordered_multiset equal_range()用法及代码示例


unordered_multiset::equal_range()是C++ STL中的内置函数,该函数返回所有元素均等于给定值的范围。它返回一对迭代器,其中第一个是指向范围下限的迭代器,第二个是指向范围上限的迭代器。如果容器中没有等于给定值的元素,则它将返回一对上下限都指向容器末端之后的位置的对。

用法:

unordered_multiset_name.equal_range(value)

参数:该函数接受要返回其范围的元素val。


返回值:它返回一对迭代器。

以下示例程序旨在说明上述函数:

程序1:

// C++ program to illustrate the 
// unordered_multiset::equal_range() function 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_multiset<int> sample; 
  
    // inserts element 
    sample.insert(100); 
    sample.insert(100); 
    sample.insert(100); 
    sample.insert(200); 
    sample.insert(500); 
    sample.insert(500); 
    sample.insert(600); 
  
    // iterator of pairs pointing to range 
    // which includes 500 and print by iterating in range 
    auto itr = sample.equal_range(500); 
    for (auto it = itr.first; it != itr.second; it++) { 
        cout << *it << " "; 
    } 
  
    cout << endl; 
  
    // iterator of pairs pointing to range 
    // which includes 100 and print by iterating in range 
    itr = sample.equal_range(100); 
    for (auto it = itr.first; it != itr.second; it++) { 
        cout << *it << " "; 
    } 
  
    return 0; 
}
输出:
500 500 
100 100 100

程序2:

// C++ program to illustrate the 
// unordered_multiset::equal_range() function 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_multiset<char> sample; 
  
    // inserts element 
    sample.insert('a'); 
    sample.insert('a'); 
    sample.insert('b'); 
    sample.insert('c'); 
    sample.insert('d'); 
    sample.insert('d'); 
    sample.insert('d'); 
  
    // iterator of pairs pointing to range 
    // which includes 'a' and print by iterating in range 
    auto itr = sample.equal_range('a'); 
    for (auto it = itr.first; it != itr.second; it++) { 
        cout << *it << " "; 
    } 
  
    cout << endl; 
  
    // iterator of pairs pointing to range 
    // which includes 'd' and print by iterating in range 
    itr = sample.equal_range('d'); 
    for (auto it = itr.first; it != itr.second; it++) { 
        cout << *it << " "; 
    } 
  
    return 0; 
}
输出:
a a 
d d d


相关用法


注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 unordered_multiset equal_range() function in C++STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。