std::count, std::count_if
|
|
Esta página se ha traducido por ordenador/computador/computadora de la versión en inglés de la Wiki usando Google Translate.
La traducción puede contener errores y palabras aparatosas/incorrectas. Planea sobre el texto para ver la versión original. Puedes ayudar a corregir los errores y mejorar la traducción. Para instrucciones haz clic aquí. |
| Definido en el archivo de encabezado <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) satisfacer criterios específicos. La primera versión cuenta los elementos que son iguales a value, la segunda versión cuenta con elementos para los cuales predicado p vuelve 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.
Contenido |
[editar] Parámetros
| first, last | - | la gama de elementos a 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 | - | el valor a buscar
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 | - | Predicado unario que devuelve true para los elementos requeridos . 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. La signatura de la función predicado debe ser equivalente a: bool pred(const Type &a); La forma no necesita tener const &, pero la función no debe modificar el objeto que se ha pasado. |
| Requerimientos de tipo | ||
-InputIt debe reunir los requerimientos de InputIterator.
| ||
[editar] Valor de retorno
You can help to correct and verify the translation. Click here for instructions.
[editar] Complejidad
last - first comparaciones / aplicaciones del predicadolast - first comparisons / applications of the predicateYou can help to correct and verify the translation. Click here for instructions.
[editar] Posible implementación
| Primera versión |
|---|
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; } |
| Segunda versión |
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] Ejemplo
count utiliza para determinar cómo muchos enteros en un std::vector de acuerdo con un valor objetivo .
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'; }
Salida:
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'; }
Salida:
number divisible by three: 3