std::count, std::count_if — cppreference.com
Espaces de noms
Variantes

std::count, std::count_if

De cppreference.com

<metanoindex/>

 
 
Bibliothèque d'algorithmes
Fonctions
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Non-modification de la séquence des opérations
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.
Modification de la séquence des opérations
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.
Des opérations de partitionnement
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.
Opérations de tri (sur les gammes triés)
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.
is_sorted (C++11)
is_sorted_until (C++11)
sort
Opérations binaires de recherche (sur les gammes triés)
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.
Définir les opérations (sur les gammes triés)
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.
Opérations Heap
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 de fonctionnement
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.
Opérations numériques
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.
Bibliothèque C
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.
 
<tbody> </tbody>
Déclaré dans l'en-tête <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)
Retourne le nombre d'éléments dans la gamme [first, last) satisfaisant des critères spécifiques. La première version compte les éléments qui sont égaux à value, la deuxième version compte les éléments pour lesquels prédicat retourne p true .
Original:
Returns the number of elements in the range [first, last) satisfying specific criteria. The first version counts the elements that are equal to value, the second version counts elements for which predicate p returns true.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Paramètres

first, last -
l'éventail des éléments à examiner
Original:
the range of elements to examine
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
value -
la valeur à rechercher
Original:
the value to search for
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
p - prédicat unéaire qui retourne ​true
pour les éléments requis
Original:
for the required elements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
.

L'expression p(v) doit pouvoir être convertie à bool pour tout argument v de type (possiblement const) VT, où VT est le type de valeur de InputIt, inconditionellement de value category, et ne doit pas modifier v. Ainsi, un type-paramètre VT&n'est pas permis ainsi que pour VT sauf pour VT un déplacement est équivalent à une copie (depuis C++11). ​

Type requirements
-
InputIt must meet the requirements of InputIterator.

Retourne la valeur

certain nombre d'éléments satisfaisant la condition .
Original:
number of elements satisfying the condition.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Complexité

exactement last - first comparaisons / applications du prédicat
Original:
exactly last - first comparisons / applications of the predicate
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Mise en œuvre possible

Exemple

Au code count utilise pour déterminer le nombre de nombres entiers dans un std::vector correspond à une valeur cible .
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';
}

Résultat :

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

Cet exemple utilise un expression lambda de compter des éléments divisible par 3 .
Original:
This example uses a expression lambda 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';
}

Résultat :

number divisible by three: 3