Espaços nominais
Variantes
Acções

std::count, std::count_if

Da cppreference.com
< cpp‎ | algorithm

 
 
Biblioteca algoritmo
Funções
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Não modificar operações de seqüência
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.
(C++11)(C++11)(C++11)
countcount_if
Modificando operações de seqüência
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.
Particionamento operações
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.
Operações de classificação (em intervalos ordenados)
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.
Binários operações de busca (em intervalos ordenados)
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.
Definir operações (em intervalos ordenados)
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.
Operações de pilha
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.
Mínimo / máximo de operações
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.
Operações numéricas
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 biblioteca
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.
 
Definido no cabeçalho <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)
Retorna o número de elementos no intervalo [first, last) satisfazer critérios específicos. A primeira versão conta os elementos que são iguais para value, a segunda versão conta com elementos para os quais predicado p retornos 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.

Índice

[editar]Parâmetros

first, last -
a gama de elementos para examinar
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 -
o valor a ser procurado
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
para os elementos necessários
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.
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.

[editar]Valor de retorno

número de elementos que satisfaz a condição.
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.

[editar]Complexidade

exatamente last - first comparações / aplicações do predicado
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.

[editar]Possível implementação

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;}

[editar]Exemplo

O código a seguir usa count para determinar quantos números inteiros em uma std::vector corresponder a um valor alvo .
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';}

Saída:

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

Este exemplo usa um lambda expressão contar elementos divisível por 3 .
Original:
This example uses a lambda expressão 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';}

Saída:

number divisible by three: 3
close