Namensräume
Varianten
Aktionen

std::count, std::count_if

Aus cppreference.com
< cpp‎ | algorithm
 
 
Algorithm Bibliothek
Funktionen
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Nicht-modifizierende Sequenz Operationen
Original:
Non-modifying sequence operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
all_of
any_of
none_of
(C++11)
(C++11)
(C++11)
for_each
count
count_if
mismatch
equal
Modifizierende Sequenz Operationen
Original:
Modifying sequence operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Partitionierungsoperationen
Original:
Partitioning operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Sortierung Operationen (auf sortierten Bereiche)
Original:
Sorting operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Binary Suchaktionen (auf sortierten Bereiche)
Original:
Binary search operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Set-Operationen (auf sortierten Bereiche)
Original:
Set operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Heap-Operationen
Original:
Heap operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Minimum / Maximum Operationen
Original:
Minimum/maximum operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Numerische Operationen
Original:
Numeric operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
C-Bibliothek
Original:
C library
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
 
Defined in header <algorithm>
template< class InputIt, class T >

typename iterator_traits<InputIt>::difference_type

    count( InputIt first, InputIt last, const T &value );
(1)
template< class InputIt, class UnaryPredicate >

typename iterator_traits<InputIt>::difference_type

    count_if( InputIt first, InputIt last, UnaryPredicate p );
(2)

Gibt die Anzahl der Elemente im Bereich [first, last) erfüllen bestimmte Kriterien. Die erste Version zählt die Elemente, die gleich value sind, zählt die zweite Version Elemente, für die Prädikats p kehrt true .

Inhaltsverzeichnis

[Bearbeiten] Parameter

first, last - das Spektrum der Elemente zu untersuchen
value - der Wert für die Suche
p - unary predicate which returns ​true für die erforderlichen Elemente.

The signature of the predicate function should be equivalent to the following:

 bool pred(const Type &a);

The signature does not need to have const &, but the function must not modify the objects passed to it.
The type Type must be such that an object of type InputIt can be dereferenced and then implicitly converted to Type. ​

Type requirements
-
InputIt must meet the requirements of InputIterator.

[Bearbeiten] Rückgabewert

Anzahl der Elemente, die der Bedingung entsprechen.

[Bearbeiten] Komplexität

genau last - first Vergleiche / Anwendungen des Prädikats.

[Bearbeiten] Mögliche Implementierung

First version
template<class InputIt, class T>
typename iterator_traits<InputIt>::difference_type
    count(InputIt first, InputIt last, const T& value)
{
    typename iterator_traits<InputIt>::difference_type ret = 0;
    for (; first != last; ++first) {
        if (*first == value) {
            ret++;
        }
    }
    return ret;
}
Second version
template<class InputIt, class UnaryPredicate>
typename iterator_traits<InputIt>::difference_type
    count_if(InputIt first, InputIt last, UnaryPredicate p)
{
    typename iterator_traits<InputIt>::difference_type ret = 0;
    for (; first != last; ++first) {
        if (p(*first)) {
            ret++;
        }
    }
    return ret;
}

[Bearbeiten] Beispiel

Der folgende Code verwendet count um zu bestimmen, wie viele Zahlen in einem std::vector mit einem Zielwert übereinstimmen.
Original:
The following code uses count to determine how many integers in a std::vector match a target value.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <algorithm>
#include <iostream>
#include <vector>
 
int main()
{
    int data[] = { 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };
    std::vector<int> v(data, data+10);
 
    int target1 = 3;
    int target2 = 5;
    int num_items1 = std::count(v.begin(), v.end(), target1);
    int num_items2 = std::count(v.begin(), v.end(), target2);
 
    std::cout << "number: " << target1 << " count: " << num_items1 << '\n';
    std::cout << "number: " << target2 << " count: " << num_items2 << '\n';
}

Output:

number: 3 count: 2
number: 5 count: 0

Dieses Beispiel verwendet einen Lambda-Ausdruck, um die durch 3 teilbaren Elemente zu zählen.
Original:
This example uses a Lambda-Ausdruck to count elements divisible by 3.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <algorithm>
#include <iostream>
#include <vector>
 
int main()
{
    int data[] = { 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };
    std::vector<int> v(data, data+10);
 
    int num_items1 = std::count_if(v.begin(), v.end(), [](int i) {return i % 3 == 0;});
 
    std::cout << "number divisible by three: " << num_items1 << '\n';
}

Output:

number divisible by three: 3