count
描述 (Description)
它用于搜索值为k的元素容器,并返回找到的元素数
声明 (Declaration)
以下是std :: unordered_set :: count的声明。
C++11
size_type count ( const key_type& k ) const;
参数 (Parameters)
k - K是搜索元素。
返回值
如果找到值等于k的元素,则返回,否则返回零。
异常 (Exceptions)
如果任何元素比较对象抛出异常,则抛出异常。
请注意,无效参数会导致未定义的行为。
时间复杂
恒定时间。
例子 (Example)
以下示例显示了std :: unordered_set :: count的用法。
#include <iostream>
#include <string>
#include <unordered_set>
int main () {
std::unordered_set<std::string> myset = { "sairam", "krishna", "prasad" };
for (auto& x: {"iowiki","sairam","krishna","t-shirt"}) {
if (myset.count(x)>0)
std::cout << "myset has " << x << std::endl;
else
std::cout << "myset has no " << x << std::endl;
}
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 -
myset has no iowiki
myset has sairam
myset has krishna
myset has no t-shirt