std::reference_wrapper - cppreference.com
De cppreference.com
|
|
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 |
||
|
|
(desde C++11) | |
std::reference_wrapper clase de plantilla es una CopyConstructible y la envoltura alrededor CopyAssignable una referencia al objeto o referencia a funcionar de T tipo. Las instancias de std::reference_wrapper son objetos (puede ser copiado o almacenado en contenedores), pero son implícitamente convertible a T&, de modo que puedan ser utilizados como argumentos con las funciones que toman el tipo subyacente por referencia .
Original:
Class template std::reference_wrapper is a CopyConstructible and CopyAssignable wrapper around a reference to object or reference to function of type T. Instances of std::reference_wrapper are objects (can be copied or stored in containers), but they are implicitly convertible to T&, so that they can be used as arguments with the functions that take the underlying type by reference.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Las funciones de ayuda std::ref y std::cref se utilizan a menudo para generar std::reference_wrapper objetos .
Original:
Helper functions std::ref and std::cref are often used to generate std::reference_wrapper objects.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
std::reference_wrapper también se utiliza para pasar objetos a std::bind o al constructor de std::thread por referencia .
Original:
std::reference_wrapper is also used to pass objects to std::bind or to the constructor of std::thread by reference.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Tipos de miembros
tipo Original: type The text has been machine-translated via Google Translate. |
definition |
type
|
T
|
result_type
|
El tipo de retorno de Original: The return type of The text has been machine-translated via Google Translate. |
argument_type
|
1) si 2) si
3) si Original: 1) if 2) if
3) if The text has been machine-translated via Google Translate. |
first_argument_type
|
1) si 2) si Original: 1) if 2) if The text has been machine-translated via Google Translate. |
second_argument_type
|
1) si 2) si
3) si Original: 1) if 2) if
3) if The text has been machine-translated via Google Translate. |
Las funciones miembro
almacena una referencia a un objeto std::reference_wrapper nuevo Original: stores a reference in a new std::reference_wrapper object The text has been machine-translated via Google Translate. (función miembro pública) [editar] | |
vuelve a enlazar una std::reference_wrapper Original: rebinds a std::reference_wrapper The text has been machine-translated via Google Translate. (función miembro pública) [editar] | |
accede a la referencia almacenada Original: accesses the stored reference The text has been machine-translated via Google Translate. (función miembro pública) [editar] | |
| llama a la función almacenada (función miembro pública) [editar] |
Ejemplo
Muestra el uso de reference_wrapper como un contenedor de referencias, lo que hace posible acceder al mismo recipiente utilizando varios índices
Original:
Demonstrates the use of reference_wrapper as a container of references, which makes it possible to access the same container using multiple indexes
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 <list> #include <vector> #include <iostream> #include <functional> int main() { std::list<int> l = {-4, -3, -2, -1, 0, 1, 2, 3, 4}; std::vector<std::reference_wrapper<int>> v(l.begin(), l.end()); std::random_shuffle(v.begin(), v.end()); std::vector<std::reference_wrapper<int>> v2(v.begin(), v.end()); std::partition(v2.begin(), v2.end(), [](int n){return n<0;}); std::cout << "Contents of the list: "; for(int n: l) { std::cout << n << ' '; } std::cout << '\n'; std::cout << "Contents of the list, shuffled: "; for(int i: v) { std::cout << i << ' '; } std::cout << '\n'; std::cout << "Shuffled elements, partitioned: "; for(int i: v2) { std::cout << i << ' '; } std::cout << '\n'; }
Salida:
Contents of the list: -4 -3 -2 -1 0 1 2 3 4 Contents of the list, shuffled: 0 -1 3 4 -4 1 -2 -3 2 Shuffled elements, partitioned: -3 -1 -2 -4 4 1 3 0 2
Ver también
| Crea un envoltorio de referencia (std::reference_wrapper) con un tipo deducido de su argumento. (plantilla de función) [editar] | |
| Vincula uno o más argumentos a un objeto función. (plantilla de función) [editar] |