GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • TUTORIALS
  • REFERENCE
  • ARTICLES
  • FORUM
  • C library:
  • <cassert> (assert.h)
  • <cctype> (ctype.h)
  • <cerrno> (errno.h)
  • <cfenv> (fenv.h)
  • <cfloat> (float.h)
  • <cinttypes> (inttypes.h)
  • <ciso646> (iso646.h)
  • <climits> (limits.h)
  • <clocale> (locale.h)

This view

  • Original page
  • New address

GeezerVison is showing cplusplus.com — std::valarray::apply

  • TUTORIALS
  • REFERENCE
  • ARTICLES
  • FORUM

C++

  • Tutorials
  • Reference
  • Articles
  • Forum

Reference

  • C library:

    • <cassert> (assert.h)
    • <cctype> (ctype.h)
    • <cerrno> (errno.h)
    • C++11
      <cfenv> (fenv.h)
    • <cfloat> (float.h)
    • C++11
      <cinttypes> (inttypes.h)
    • <ciso646> (iso646.h)
    • <climits> (limits.h)
    • <clocale> (locale.h)
    • <cmath> (math.h)
    • <csetjmp> (setjmp.h)
    • <csignal> (signal.h)
    • <cstdarg> (stdarg.h)
    • C++11
      <cstdbool> (stdbool.h)
    • <cstddef> (stddef.h)
    • C++11
      <cstdint> (stdint.h)
    • <cstdio> (stdio.h)
    • <cstdlib> (stdlib.h)
    • <cstring> (string.h)
    • C++11
      <ctgmath> (tgmath.h)
    • <ctime> (time.h)
    • C++11
      <cuchar> (uchar.h)
    • <cwchar> (wchar.h)
    • <cwctype> (wctype.h)
  • Containers:

    • C++11
      <array>
    • <deque>
    • C++11
      <forward_list>
    • <list>
    • <map>
    • <queue>
    • <set>
    • <stack>
    • C++11
      <unordered_map>
    • C++11
      <unordered_set>
    • <vector>
  • Input/Output:

    • <fstream>
    • <iomanip>
    • <ios>
    • <iosfwd>
    • <iostream>
    • <istream>
    • <ostream>
    • <sstream>
    • <streambuf>
  • Multi-threading:

    • C++11
      <atomic>
    • C++11
      <condition_variable>
    • C++11
      <future>
    • C++11
      <mutex>
    • C++11
      <thread>
  • Other:

    • <algorithm>
    • <bitset>
    • C++11
      <chrono>
    • C++11
      <codecvt>
    • <complex>
    • <exception>
    • <functional>
    • C++11
      <initializer_list>
    • <iterator>
    • <limits>
    • <locale>
    • <memory>
    • <new>
    • <numeric>
    • C++11
      <random>
    • C++11
      <ratio>
    • C++11
      <regex>
    • <stdexcept>
    • <string>
    • C++11
      <system_error>
    • C++11
      <tuple>
    • C++11
      <type_traits>
    • C++11
      <typeindex>
    • <typeinfo>
    • <utility>
    • <valarray>

<valarray>

  • classes

    • gslice
    • gslice_array
    • indirect_array
    • mask_array
    • slice
    • slice_array
    • valarray
  • functions

    • abs
    • acos
    • asin
    • atan
    • atan2
    • cos
    • cosh
    • exp
    • log
    • log10
    • pow
    • sin
    • sinh
    • sqrt
    • tan
    • tanh

valarray

  • valarray::~valarray
  • valarray operators
  • valarray::valarray
  • member functions

    • valarray::apply
    • valarray::cshift
    • valarray::max
    • valarray::min
    • valarray::operator[]
    • valarray::operator=
    • valarray::resize
    • valarray::shift
    • valarray::size
    • valarray::sum
    • C++11
      valarray::swap
  • non-member overloads

    • C++11
      begin (valarray)
    • C++11
      end (valarray)
    • C++11
      swap (valarray)
  • Reference
  • <valarray>
  • valarray
  • apply
public member function
<valarray>

std::valarray::apply

valarray apply (T func(T)) const;valarray apply (T func(const T&)) const;
Apply function
Returns a valarray with each of its elements initialized to the result of applying func to its corresponding element in *this.

The valarray returned has the same length as *this.

Parameters

func
Pointer to a function taking an argument of type T (or a constant reference) and returning T.
T is the template argument of valarray (the value type).

Return value

A valarray object with the results of applying func to all the elements of *this.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// valarray::apply example
#include <iostream>     // std::cout
#include <cstddef>      // std::size_t
#include <valarray>     // std::valarray

int increment (int x) {return ++x;}

int main ()
{
  int init[]={10,20,30,40,50};
  std::valarray<int> foo (init,5);

  std::valarray<int> bar = foo.apply(increment);

  std::cout << "foo contains:";
  for (std::size_t n=0; n<bar.size(); n++)
	  std::cout << ' ' << bar[n];
  std::cout << '\n';

  return 0;
}

Output:

11 21 31 41 51


Complexity

Depends on library implementation (operations may be parallelized).

Iterator validity

No changes: Valid iterators, references and sub-arrays keep their validity.

Data races

Both the valarray and its elements are accessed.

Exception safety

If any operation performed on the elements throws an exception, it causes undefined behavior.
If the function fails to allocate storage it may throw an exception (such as bad_alloc), although this is not mandated.

See also

valarray::operator=
Assign content (public member function)
valarray operators
Valarray operators (function)
Home page | Privacy policy
© cplusplus.com, 2000-2026 - All rights reserved - v3.3.4s
Spotted an error? contact us
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.