Cpp count set std

std::set:: count

Returns the number of elements with key that compares equivalent to the specified argument.

1) Returns the number of elements with key key . This is either 1 or 0 since this container does not allow duplicates.

2) Returns the number of elements with key that compares equivalent to the value x . This overload participates in overload resolution only if the qualified-id Compare :: is_transparent is valid and denotes a type. It allows calling this function without constructing an instance of Key .

Contents

[edit] Parameters

[edit] Return value

Number of elements with key that compares equivalent to key or x , which, for overload (1) , is either 1 or 0.

[edit] Complexity

Logarithmic in the size of the container plus linear in the number of elements found.

[edit] Example

#include #include 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::setint> t{3, 1, 4, 1, 5}; std::cout  t.count(1)  ", "  t.count(2)  ".\n"; std::setS> s{3, 1, 4, 1, 5}; std::cout  ": "  s.count(1)  ", "  s.count(2)  ".\n"; // Two temporary objects S and S were created. // Comparison function object is defaulted std::less, // which is not transparent (has no is_transparent member type). std::setR, 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 has predefined is_transparent. }
1, 0. S S S S S : S 1, S 0. R R R R R : 1, 0.

Источник

std:: set::count

Searches the container for elements equivalent to val and returns the number of matches.

Because all elements in a set container are unique, the function can only return 1 (if the element is found) or zero (otherwise).

Two elements of a set are considered equivalent if the container’s comparison object returns false reflexively (i.e., no matter the order in which the elements are passed as arguments).

Parameters

val Value to search for.
Member type value_type is the type of the elements in the container, defined in set as an alias of its first template parameter (T).

Return value

1 if the container contains an element equivalent to val, or zero otherwise.

Member type size_type is an unsigned integral type.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// set::count int main () < std::setint> myset; // set some initial values: for (int i=1; i// set: 3 6 9 12 for (int i=0; iif (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 an element of myset. 4 is not an element of myset. 5 is not an element of myset. 6 is an element of myset. 7 is not an element of myset. 8 is not an element of myset. 9 is an element of myset. 

Complexity

Iterator validity

Data races

Exception safety

See also

set::find Get iterator to element (public member function) set::size Return container size (public member function) set::equal_range Get range of equal elements (public member function)

Источник

std::set count() method

Returns the number of elements with key that compares equivalent to the specified argument, which is either 1 or 0 since this container does not allow duplicates.

  • (1) Returns the number of elements with key key .
  • (2) Returns the number of elements with key that compares equivalent to the value x . This overload participates in overload resolution only if the qualified-id Compare::is_transparent is valid and denotes a type. They allow calling this function without constructing an instance of Key .

Parameters​

  • key — key value of the elements to count
  • x — a value of any type that can be transparently compared with a key

Return value​

Number of elements with key that compares equivalent to key or x , which is either 1 or 0 for (1).

Complexity​

Logarithmic in the size of the container — O(log size()).

Exceptions​

Example​

 #include  #include   struct S    int x;   S(int i) : xi>  std::cout  <"S < i  <"> "; >   bool operator(S const& s) const  return x  s.x; >   >;    struct R    int x;   R(int i) : xi>  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::setint> t3, 1, 4, 1, 5>;   std::cout < t.count(1)  <", " < t.count(2)  <".\n";    std::setS> s3, 1, 4, 1, 5>;   std::cout  <": " < s.count(1)  <", " < s.count(2)  <".\n";   // Two temporary objects S and S were created.   // Comparison function object is defaulted std::less,   // which is not transparent (has no is_transparent member type).    std::setR, std::less>> r3, 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 has predefined is_transparent.   > 

Источник

Читайте также:  Открыть скрипт из скрипта python
Оцените статью