跳到主要内容

std::set count() 方法

// (1) Const version
size_type count( const Key& key ) const;

// (2) Const version
template< class K >
size_type count( const K& x ) const;

返回与指定参数的键比较相等的元素数量,由于此容器不允许重复,所以结果为 10

  • (1) 返回键为 key 的元素数量。
  • (2) 返回与值 x 的键比较相等的元素数量。此重载仅当限定 id Compare::is_transparent 有效且表示一个类型时才参与重载决议。它们允许在不构造 Key 实例的情况下调用此函数。

参数

  • key - 要计数的元素的键值
  • x - 可以与键透明比较的任何类型的值

返回值

与键或 x 比较相等的元素数量,对于 (1),结果为 10

复杂度

对容器大小呈对数关系 - O(log size())

异常

(无)

示例

Main.cpp
#include <set>
#include <iostream>

struct S {
int x;
S(int i) : x{i} { std::cout << "S{" << i << "} "; }
bool operator<(S const& s) const { return x < s.x; }
};

struct R {
int x;
R(int i) : x{i} { std::cout << "R{" << i << "} "; }
bool operator<(R const& r) const { return x < r.x; }
};
bool operator<(R const& r, int i) { return r.x < i; }
bool operator<(int i, R const& r) { return i < r.x; }

int main()
{
std::set<int> t{3, 1, 4, 1, 5};
std::cout << t.count(1) << ", " << t.count(2) << ".\n";

std::set<S> s{3, 1, 4, 1, 5};
std::cout << ": " << s.count(1) << ", " << s.count(2) << ".\n";
// Two temporary objects S{1} and S{2} were created.
// Comparison function object is defaulted std::less<S>,
// which is not transparent (has no is_transparent member type).

std::set<R, std::less<>> r{3, 1, 4, 1, 5};
std::cout << ": " << r.count(1) << ", " << r.count(2) << ".\n";
// C++14 heterogeneous lookup; temporary objects were not created.
// Comparator std::less<void> has predefined is_transparent.
}
可能输出
1, 0.
S{3} S{1} S{4} S{1} S{5} : S{1} 1, S{2} 0.
R{3} R{1} R{4} R{1} R{5} : 1, 0.
本文档源自此 CppReference 页面。它可能为了改进或编辑者的偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。

std::set count() 方法

// (1) Const version
size_type count( const Key& key ) const;

// (2) Const version
template< class K >
size_type count( const K& x ) const;

返回与指定参数的键比较相等的元素数量,由于此容器不允许重复,所以结果为 10

  • (1) 返回键为 key 的元素数量。
  • (2) 返回与值 x 的键比较相等的元素数量。此重载仅当限定 id Compare::is_transparent 有效且表示一个类型时才参与重载决议。它们允许在不构造 Key 实例的情况下调用此函数。

参数

  • key - 要计数的元素的键值
  • x - 可以与键透明比较的任何类型的值

返回值

与键或 x 比较相等的元素数量,对于 (1),结果为 10

复杂度

对容器大小呈对数关系 - O(log size())

异常

(无)

示例

Main.cpp
#include <set>
#include <iostream>

struct S {
int x;
S(int i) : x{i} { std::cout << "S{" << i << "} "; }
bool operator<(S const& s) const { return x < s.x; }
};

struct R {
int x;
R(int i) : x{i} { std::cout << "R{" << i << "} "; }
bool operator<(R const& r) const { return x < r.x; }
};
bool operator<(R const& r, int i) { return r.x < i; }
bool operator<(int i, R const& r) { return i < r.x; }

int main()
{
std::set<int> t{3, 1, 4, 1, 5};
std::cout << t.count(1) << ", " << t.count(2) << ".\n";

std::set<S> s{3, 1, 4, 1, 5};
std::cout << ": " << s.count(1) << ", " << s.count(2) << ".\n";
// Two temporary objects S{1} and S{2} were created.
// Comparison function object is defaulted std::less<S>,
// which is not transparent (has no is_transparent member type).

std::set<R, std::less<>> r{3, 1, 4, 1, 5};
std::cout << ": " << r.count(1) << ", " << r.count(2) << ".\n";
// C++14 heterogeneous lookup; temporary objects were not created.
// Comparator std::less<void> has predefined is_transparent.
}
可能输出
1, 0.
S{3} S{1} S{4} S{1} S{5} : S{1} 1, S{2} 0.
R{3} R{1} R{4} R{1} R{5} : 1, 0.
本文档源自此 CppReference 页面。它可能为了改进或编辑者的偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。