std::count, std::count_if
De cppreference.com
[] |
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
<metanoindex/>
<tbody> </tbody>| Dclar dans l'en-tte <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'lments dans la gamme
[first, last) satisfaisant des critres spcifiques. La premire version compte les lments qui sont gaux value, la deuxime version compte les lments pour lesquels prdicat 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.
You can help to correct and verify the translation. Click here for instructions.
Paramtres
| first, last | - | l'ventail des lments 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 | - | prdicat unaire qui retourne true pour les lments 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 |
| Type requirements | ||
-InputIt must meet the requirements of InputIterator.
| ||
Retourne la valeur
certain nombre d'lments 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.
You can help to correct and verify the translation. Click here for instructions.
Complexit
exactement
last - first comparaisons / applications du prdicatOriginal:
exactly
last - first comparisons / applications of the predicateThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Mise en uvre possible
| 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;
}
|
Exemple
Au code
count utilise pour dterminer 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.
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';
}
Rsultat :
number: 3 count: 2
number: 5 count: 0
Cet exemple utilise un expression lambda de compter des lments 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.
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';
}
Rsultat :
number divisible by three: 3