operator+,-,*,/,%,&,|,^,<<,>>,&&,|| (std::valarray)
De es.cppreference.net
| Definido en el encabezado <valarray>
|
||
template< class T >
std::valarray<T> operator+ ( const std::valarray<T>& lhs, const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator- ( const std::valarray<T>& lhs, const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator* ( const std::valarray<T>& lhs, const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator/ ( const std::valarray<T>& lhs, const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator% ( const std::valarray<T>& lhs, const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator& ( const std::valarray<T>& lhs, const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator| ( const std::valarray<T>& lhs, const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator^ ( const std::valarray<T>& lhs, const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator<<( const std::valarray<T>& lhs, const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator>>( const std::valarray<T>& lhs, const std::valarray<T>& rhs );
template< class T >
std::valarray<bool> operator&&( const std::valarray<T>& lhs, const std::valarray<T>& rhs );
template< class T >
std::valarray<bool> operator||( const std::valarray<T>& lhs, const std::valarray<T>& rhs );
|
(1) | |
template< class T >
std::valarray<T> operator+ ( const typename std::valarray<T>::value_type & val,
const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator- ( const typename std::valarray<T>::value_type & val,
const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator* ( const typename std::valarray<T>::value_type & val,
const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator/ ( const typename std::valarray<T>::value_type & val,
const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator% ( const typename std::valarray<T>::value_type & val,
const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator& ( const typename std::valarray<T>::value_type & val,
const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator| ( const typename std::valarray<T>::value_type & val,
const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator^ ( const typename std::valarray<T>::value_type & val,
const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator<<( const typename std::valarray<T>::value_type & val,
const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator>>( const typename std::valarray<T>::value_type & val,
const std::valarray<T>& rhs );
template< class T >
std::valarray<bool> operator&&( const typename std::valarray<T>::value_type & val,
const std::valarray<T>& rhs );
template< class T >
std::valarray<bool> operator||( const typename std::valarray<T>::value_type & val,
const std::valarray<T>& rhs );
|
(2) | |
template< class T >
std::valarray<T> operator+ ( const std::valarray<T>& lhs,
const typename std::valarray<T>::value_type & val );
template< class T >
std::valarray<T> operator- ( const std::valarray<T>& lhs,
const typename std::valarray<T>::value_type & val );
template< class T >
std::valarray<T> operator* ( const std::valarray<T>& lhs,
const typename std::valarray<T>::value_type & val );
template< class T >
std::valarray<T> operator/ ( const std::valarray<T>& lhs,
const typename std::valarray<T>::value_type & val );
template< class T >
std::valarray<T> operator% ( const std::valarray<T>& lhs,
const typename std::valarray<T>::value_type & val );
template< class T >
std::valarray<T> operator& ( const std::valarray<T>& lhs,
const typename std::valarray<T>::value_type & val );
template< class T >
std::valarray<T> operator| ( const std::valarray<T>& lhs,
const typename std::valarray<T>::value_type & val );
template< class T >
std::valarray<T> operator^ ( const std::valarray<T>& lhs,
const typename std::valarray<T>::value_type & val );
template< class T >
std::valarray<T> operator<<( const std::valarray<T>& lhs,
const typename std::valarray<T>::value_type & val );
template< class T >
std::valarray<T> operator>>( const std::valarray<T>& lhs,
const typename std::valarray<T>::value_type & val );
template< class T >
std::valarray<bool> operator&&( const std::valarray<T>& lhs,
const typename std::valarray<T>::value_type & val );
template< class T >
std::valarray<bool> operator||( const std::valarray<T>& lhs,
const typename std::valarray<T>::value_type & val );
|
(3) | |
Aplica operadores binarios a cada elemento de dos valarrays, o de un valarray y un valor.
1) Los operadores trabajan en valarrays del mismo tamaño y devuelven un valarray del mismo tamaño que los parámetros con la operación aplicada a cada elemento de los dos argumentos.
2,3) Aplica el operador entre cada elemento del valarray y el escalar.
Parámetros
| rhs | - | un arreglo numérico |
| lhs | - | un arreglo numérico |
| val | - |
un valor de tipo
T
|
Valor de retorno
Un valarray con el mismo tamaño que el parámetro.
Nota
El comportamiento no está definido cuando los dos argumentos son valarrays de diferentes tamaños.
La función puede implementarse con un tipo de retorno diferente de std::valarray. En este caso, el tipo de reemplazo tiene las siguientes propiedades:
- Se proporcionan todas
constlas funciones miembro de std::valarray. - std::valarray, std::slice_array, std::gslice_array, std::mask_array y std::indirect_array pueden construirse a partir del tipo de reemplazo.
- Para cada función que toma un
const std::valarray<T>&excepto begin() y end()(desde C++11), se añadirán funciones idénticas que tomen los tipos de reemplazo; - Para cada función que toma dos
const std::valarray<T>&argumentos, se añadirán funciones idénticas que tomen cualquier combinación deconst std::valarray<T>&y tipos de reemplazo. - El tipo de retorno no añade más de dos niveles de anidamiento de plantillas sobre el tipo de argumento más profundamente anidado.
Ejemplo
Encuentra raíces reales de múltiples ecuaciones cuadráticas .
Ejecutar este código
#include <cstddef> #include <iostream> #include <valarray> int main() { std::valarray<double> a(1, 8); std::valarray<double> b{1, 2, 3, 4, 5, 6, 7, 8}; std::valarray<double> c = -b; // los literales también deben ser de tipo T hasta LWG3074 (double en este caso) std::valarray<double> d = std::sqrt(b * b - 4.0 * a * c); std::valarray<double> x1 = 2.0 * c / (-b + d); std::valarray<double> x2 = 2.0 * c / (-b - d); std::cout << "quadratic equation: root 1: root 2: b: c:\n"; for (std::size_t i = 0; i < a.size(); ++i) std::cout << a[i] << "\u00B7x\u00B2 + " << b[i] << "\u00B7x + " << c[i] << " = 0 " << std::fixed << x1[i] << " " << x2[i] << std::defaultfloat << " " << -x1[i] - x2[i] << " " << x1[i] * x2[i] << '\n'; }
Salida:
quadratic equation: root 1: root 2: b: c: 1·x² + 1·x + -1 = 0 -1.618034 0.618034 1 -1 1·x² + 2·x + -2 = 0 -2.732051 0.732051 2 -2 1·x² + 3·x + -3 = 0 -3.791288 0.791288 3 -3 1·x² + 4·x + -4 = 0 -4.828427 0.828427 4 -4 1·x² + 5·x + -5 = 0 -5.854102 0.854102 5 -5 1·x² + 6·x + -6 = 0 -6.872983 0.872983 6 -6 1·x² + 7·x + -7 = 0 -7.887482 0.887482 7 -7 1·x² + 8·x + -8 = 0 -8.898979 0.898979 8 -8
Informes de defectos
Los siguientes informes de defectos que modifican el comportamiento se aplicaron retroactivamente a los estándares publicados anteriormente de C++.
| DR | Aplicado a | Comportamiento publicado | Comportamiento correcto |
|---|---|---|---|
| LWG 3074 | C++98 |
T
se deduce tanto del escalar como del
valarray
para
(2,3)
, impidiendo llamadas de tipos mixtos
|
deducir únicamente
T
del
valarray
|
Véase también
|
aplica un operador aritmético unario a cada elemento del valarray
(función miembro pública) |
|
|
aplica un operador de asignación compuesta a cada elemento del valarray
(función miembro pública) |