function template

std::abs

<valarray>
template<class T> valarray<T> abs (const valarray<T>& x);
Compute absolute value of valarray elements
Returns a valarray object containing the absolute values of all the elements of x.

This function is overloaded in <cstdlib> for integral types (see cstdlib abs), in <cmath> for floating-point types (see cmath abs), and in <complex> for complex numbers (see complex abs).

Parameters

x
valarray containing elements of a type for which the unary function abs is defined.

Return value

A valarray object with the absolute values of the elements of x.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// abs valarray example
#include <cstdlib>
#include <iostream>
#include <valarray>
using namespace std;

int main ()
{
  int val[] = {5, 3, -10, 4, -7};
  valarray<int> foo (val,5);

  valarray<int> bar = abs (foo);

  cout << "foo: ";
  for (size_t i=0; i<foo.size(); ++i)
	  cout << foo[i] << ' ';
  cout << endl;

  cout << "bar: ";
  for (size_t i=0; i<bar.size(); ++i)
	  cout << bar[i] << ' ';
  cout << endl;

  return 0;
}


Output:

foo: 5 3 -10 4 -7
bar: 5 3 10 4 7

See also