C++ STL中的无序命令

这个 无序的_multimap::find() 在C++ STL中是一个内置函数,它返回一个迭代器,它指向一个元素,其中包含一个键。 K .如果容器不包含任何键为k的元素,则返回一个迭代器,该迭代器指向容器中最后一个元素之后的位置。

null

语法:

unordered_multimap_name.find(k)

参数: 该函数接受一个强制参数 K 它指定了密钥。

返回值: 它返回一个迭代器,该迭代器指向具有键的元素所在的位置 K

以下程序说明了上述功能:

项目1:

// C++ program to illustrate the
// unordered_multimap::find() function
#include <iostream>
#include <unordered_map>
using namespace std;
int main()
{
// declaration
unordered_multimap< int , int > sample;
// inserts key and element
sample.insert({ 1, 2 });
sample.insert({ 1, 2 });
sample.insert({ 2, 3 });
sample.insert({ 3, 4 });
sample.insert({ 2, 6 });
// find the element with key 1 and print
auto it = sample.find(1);
if (it != sample.end())
cout << 1 << ":" << it->second << endl;
else
cout << "element with key 1 not found" ;
// find the element with
// key 2 and print
it = sample.find(2);
if (it != sample.end())
cout << 2 << ":" << it->second << endl;
else
cout << "element with key 2 not found" ;
// find the element with
// key 100 and print
it = sample.find(100);
if (it != sample.end())
cout << 100 << ":" << it->second << endl;
else
cout << "element with key 100 not found" ;
return 0;
}


输出:

1:2
2:6
element with key 100 not found

项目2:

// C++ program to illustrate the
// unordered_multimap::find()
#include <iostream>
#include <unordered_map>
using namespace std;
int main()
{
// declaration
unordered_multimap< char , char > sample;
// inserts element
sample.insert({ 'a' , 'b' });
sample.insert({ 'a' , 'b' });
sample.insert({ 'a' , 'd' });
sample.insert({ 'b' , 'e' });
sample.insert({ 'b' , 'd' });
// find the element with
// key r and print
auto it = sample.find( 'r' );
if (it != sample.end())
cout << "r"
<< ":" << it->second << endl;
else
cout << "element with key r not found" ;
// find the element with
// key a and print
it = sample.find( 'a' );
if (it != sample.end())
cout << 'a' << ":" << it->second << endl;
else
cout << "element with key a not found" ;
// find the element with
// key 'b' and print
it = sample.find( 'b' );
if (it != sample.end())
cout << "b"
<< ":" << it->second << endl;
else
cout << "element with key b not found" ;
return 0;
}


输出:

element with key r not found
a:d
b:d

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享