C++ Set 库 - count 函数



描述

它搜索容器中与 val 等价的元素,并返回匹配的数量。

声明

以下是 std::set::count 在不同 C++ 版本中的工作方式。

C++98

size_type count (const value_type& val) const;

C++11

size_type count (const value_type& val) const;

返回值

它返回匹配的数量。

异常

如果抛出异常,容器中不会有任何更改。

时间复杂度

时间复杂度取决于对数。

示例

以下示例显示了 std::set::count 的用法。

#include <iostream>
#include <set>

int main () {
   std::set<int> myset;

   for (int i = 1; i < 15;++i) myset.insert(i*5);

   for (int i = 0; i < 5; ++i) {
      std::cout << i;
      if (myset.count(i)!=0)
         std::cout << " is an element of myset.\n";
      else
         std::cout << " is not an element of myset.\n";
   }

   return 0;
}

以上程序将编译并正确执行。

0 is not an element of myset.
1 is not an element of myset.
2 is not an element of myset.
3 is not an element of myset.
4 is not an element of myset.
set.htm
广告

© . All rights reserved.