std::count, std::count_if
![]() | Questa pagina è stata tradotta in modo automatico dalla versione in ineglese della wiki usando Google Translate. La traduzione potrebbe contenere errori e termini strani. Muovi il puntatore sopra al testo per vedere la versione originale. Puoi aiutarci a correggere gli gli errori. Per ulteriori istruzioni clicca qui. |
Defined in header <algorithm> | ||
template<class InputIt, class T > typename iterator_traits<InputIt>::difference_type | (1) | |
template<class InputIt, class UnaryPredicate > typename iterator_traits<InputIt>::difference_type | (2) | |
[first, last)
soddisfano criteri specifici. La prima versione conta gli elementi che sono uguali a value
, la seconda versione conta elementi per i quali predicato p
ritorna true. [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. You can help to correct and verify the translation. Click here for instructions.
Indice |
[modifica]Parametri
first, last | - | la gamma di elementi da esaminare 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 | - | il valore da cercare 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 | - | unary predicate which returns true per gli elementi richiesti . 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. 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. |
Type requirements | ||
-InputIt must meet the requirements of InputIterator . |
[modifica]Valore di ritorno
You can help to correct and verify the translation. Click here for instructions.
[modifica]Complessità
last
- first
confronto / applicazioni del predicatolast
- first
comparisons / applications of the predicateYou can help to correct and verify the translation. Click here for instructions.
[modifica]Possibile implementazione
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;} |
[modifica]Esempio
count
utilizza per determinare quanti numeri interi in un std::vector corrisponde un valore obiettivo . count
to determine how many integers in a std::vector match a target value. 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
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