C++中的无序映射映射()

OnDealdEyMava::CuffTy()是C++中的一种构建方法,用于计算给定键中无序元素的映射元素的数量。

null

笔记 :由于无序_映射不允许存储具有重复键的元素,因此count()函数基本上检查无序_映射中是否存在具有给定键的元素。

语法 :

size_type count(Key);

参数 :此函数接受单个参数 钥匙 需要在给定的无序映射容器中进行检查。

返回值 :如果给定键的映射中存在值,则此函数返回1,否则返回0。

下面的程序演示了无序的_map::count()函数:

方案1 :

// C++ program to illustrate the
// unordered_map::count() function
#include<iostream>
#include<unordered_map>
using namespace std;
int main()
{
// unordered map
unordered_map< int , string> umap;
// Inserting elements into the map
umap.insert(make_pair(1, "Welcome" ));
umap.insert(make_pair(2, "to" ));
umap.insert(make_pair(3, "GeeksforGeeks" ));
// Check if element with key 1 is present using
// count() function
if (umap.count(1))
{
cout<< "Element Found" <<endl;
}
else
{
cout<< "Element Not Found" <<endl;
}
return 0;
}


输出:

Element Found

方案2 :

// C++ program to illustrate the
// unordered_map::count() function
#include<iostream>
#include<unordered_map>
using namespace std;
int main()
{
// unordered map
unordered_map< int , string> umap;
// Inserting elements into the map
umap.insert(make_pair(1, "Welcome" ));
umap.insert(make_pair(2, "to" ));
umap.insert(make_pair(3, "GeeksforGeeks" ));
// Try inserting element with
// duplicate keys
umap.insert(make_pair(3, "CS Portal" ));
// Print the count of values with key 3
// to check if duplicate values are stored
// or not
cout<< "Count of elements in map, mapped with key 3: "
<<umap.count(3);
return 0;
}


输出:

Count of elements in map, mapped with key 3: 1

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