📜  C++ STL中的unordered_multiset equal_range()函数

📅  最后修改于: 2021-05-30 04:47:00             🧑  作者: Mango

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 
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multiset 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 
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multiset 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
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”