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

📅  最后修改于: 2021-05-30 08:12:49             🧑  作者: Mango

unordered_multiset :: find()是C++ STL中的内置函数,该函数返回一个迭代器,该迭代器指向具有val元素的位置。如果该元素不包含元素val ,则它将返回一个迭代器,该迭代器指向容器中最后一个元素之后的位置。

句法:

unordered_multiset_name.find(val)

参数:该函数接受一个强制参数val ,该参数的val迭代器将被返回。

返回值:返回一个迭代器,该迭代器指向val所在的位置。

下面的程序说明了上述函数:

程序1:

// C++ program to illustrate the
// unordered_multiset::find() 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);
  
    // find the position of 500 and print
    auto it = sample.find(500);
    if (it != sample.end())
        cout << *it << endl;
    else
        cout << "500 not found\n";
  
    // find the position of 300 and print
    it = sample.find(300);
    if (it != sample.end())
        cout << *it << endl;
    else
        cout << "300 not found\n";
  
    // find the position of 100 and print
    it = sample.find(100);
    if (it != sample.end())
        cout << *it << endl;
    else
        cout << "100 not found\n";
  
    return 0;
}
输出:
500
300 not found
100

程式2:

// C++ program to illustrate the
// unordered_multiset::find() 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');
  
    // find the position of 'a' and print
    auto it = sample.find('a');
    if (it != sample.end())
        cout << *it << endl;
    else
        cout << "a not found\n";
  
    // find the position of 'z' and print
    it = sample.find('z');
    if (it != sample.end())
        cout << *it << endl;
    else
        cout << "z not found\n";
  
    return 0;
}
输出:
a
z not found
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”