Espacios de nombres
Variantes
Acciones

std::transform

De cppreference.com
< cpp‎ | algorithm

 
 
Algoritmo biblioteca
Funciones
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Para no modificar la secuencia de las operaciones
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.
Modificación de la secuencia de operaciones
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.
Particionamiento operaciones
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.
Clasificación de las operaciones (en rangos 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.
Las operaciones binarias de búsqueda (en rangos 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.
Conjunto de operaciones (en rangos 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.
Operaciones del montón
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 operaciones
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.
Operaciones 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.
 
Defined in header <algorithm>
template< class InputIt, class OutputIt, class UnaryOperation >

OutputIt transform( InputIt first1, InputIt last1, OutputIt d_first,

                    UnaryOperation unary_op );
(1)
template< class InputIt1, class InputIt2, class OutputIt, class BinaryOperation >

OutputIt transform( InputIt1 first1, InputIt1 last1, InputIt2 first2,

                    OutputIt d_first, BinaryOperation binary_op );
(2)
std::transform aplica la función dada a una amplia y almacena el resultado en otra gama, a partir de d_first .
Original:
std::transform applies the given function to a range and stores the result in another range, beginning at d_first.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
En la primera versión unary_op operación unaria se aplica al rango definido por [first1, last1). En la segunda versión el binary_op operación binaria se aplica a los pares de elementos de dos gamas: una de ellas definida por [first1, last1) y el otro comienzo en first2 .
Original:
In the first version unary operation unary_op is applied to the range defined by [first1, last1). In the second version the binary operation binary_op is applied to pairs of elements from two ranges: one defined by [first1, last1) and the other beginning at first2.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Contenido

[editar] Parámetros

first1, last1 -
la primera gama de elementos a transformar
Original:
the first range of elements to transform
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
first2 -
el comienzo de la segunda gama de elementos a transformar
Original:
the beginning of the second range of elements to transform
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
d_first -
el comienzo del rango de destino, puede ser igual a first1 o first2
Original:
the beginning of the destination range, may be equal to first1 or first2
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
unary_op - unary operation function object that will be applied.

The signature of the function should be equivalent to the following:

 Ret fun(const Type &a);

The signature does not need to have const &.
The type  Type must be such that an object of type InputIt can be dereferenced and then implicitly converted to  Type. The type  Ret must be such that an object of type OutputIt can be dereferenced and assigned a value of type  Ret. ​

binary_op - binary operation function object that will be applied.

The signature of the function should be equivalent to the following:

 Ret fun(const Type1 &a, const Type2 &b);

The signature does not need to have const &.
The types  Type1 and  Type2 must be such that objects of types InputIt1 and InputIt2 can be dereferenced and then implicitly converted to  Type1 and  Type2 respectively. The type  Ret must be such that an object of type OutputIt can be dereferenced and assigned a value of type  Ret. ​

Type requirements
-
InputIt must meet the requirements of InputIterator.
-
InputIt1 must meet the requirements of InputIterator.
-
InputIt2 must meet the requirements of InputIterator.
-
OutputIt must meet the requirements of OutputIterator.

[editar] Valor de retorno

iterador de salida para el elemento pasado el último elemento transformado .
Original:
output iterator to the element past the last element transformed.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[editar] Complejidad

1)
exactamente std::distance(first1, last1) aplicaciones de unary_op
Original:
exactly std::distance(first1, last1) applications of unary_op
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
exactamente std::distance(first1, last1) aplicaciones de binary_op
Original:
exactly std::distance(first1, last1) applications of binary_op
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[editar] Requisitos

unary_op binary_op y no tienen efectos secundarios. (hasta C++11)
Original:
unary_op and binary_op have no side effects. (hasta C++11)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
unary_op y binary_op no invalidan cualquier iteradores, incluidos los iteradores finales, o modificar cualquier elemento de los rangos involucrados. (desde C++11)
Original:
unary_op and binary_op do not invalidate any iterators, including the end iterators, or modify any elements of the ranges involved. (desde C++11)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
El propósito de estos requisitos es permitir implementaciones paralelas o fuera de orden, de std::transform. Para aplicar una función a una secuencia en orden, utilizar std::for_each .
Original:
The intent of these requirements is to allow parallel or out-of-order implementations of std::transform. To apply a function to a sequence in-order, use std::for_each.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[editar] Posible implementación

First version
template<class InputIt, class OutputIt, class UnaryOperation>
OutputIt transform(InputIt first1, InputIt last1, OutputIt d_first, 
                   UnaryOperation unary_op)
{
    while (first1 != last1) {
        *d_first++ = unary_op(*first1++);
    }
    return d_first;
}
Second version
template<class InputIt1, class InputIt2, 
         class OutputIt, class BinaryOperation>
OutputIt transform(InputIt first1, InputIt last1, InputIt first2, 
                   OutputIt d_first, BinaryOperation binary_op)
{
    while (first1 != last1) {
        *d_first++ = binary_op(*first1++, *first2++);
    }
    return d_first;
}

[editar] Ejemplo

El código siguiente utiliza transformar para convertir una cadena a mayúsculas utilizando la función toupper:
Original:
The following code uses transform to convert a string to uppercase using the toupper function:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <string>
#include <cctype>
#include <algorithm>
#include <iostream>
 
int main()
{
    std::string s("hello");
    std::transform(s.begin(), s.end(), s.begin(), (int (*)(int))std::toupper);
    std::cout << s;
 }

Output:

HELLO

[editar] Ver también

se aplica una función a una serie de elementos
Original:
applies a function to a range of elements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(función de plantilla) [edit]