GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::valarray::apply(3) C++ Standard Libary std::valarray::apply(3)

std::valarray::apply - std::valarray::apply


valarray<T> apply( T func(T) ) const;
valarray<T> apply( T func(const T&) ) const;


Returns a new valarray of the same size with values which are acquired by applying
function func to the previous values of the elements.


func - function to apply to the values


The resulting valarray with values acquired by applying function func.


The function can be implemented with the return type different from std::valarray.
In this case, the replacement type has the following properties:


* All const member functions of std::valarray are provided.
* std::valarray, std::slice_array, std::gslice_array, std::mask_array and
std::indirect_array can be constructed from the replacement type.
* All functions accepting an argument of type const std::valarray&
except begin() and end()
(since C++11) should also accept the replacement type.
* All functions accepting two arguments of type const std::valarray&
should accept every combination of const std::valarray& and the
replacement type.
* The return type does not add more than two levels of template nesting
over the most deeply-nested argument type.


Following straightforward implementations can be replaced by expression templates
for a higher efficiency.


template<class T>
valarray<T> valarray<T>::apply( T func(T) ) const
{
valarray<T> other = *this;
for (T &i : other) {
i = func(i);
}
return other;
}


template<class T>
valarray<T> valarray<T>::apply( T func(const T&) ) const
{
valarray<T> other = *this;
for (T &i : other) {
i = func(i);
}
return other;
}


calculates and prints the first 10 factorials

// Run this code


#include <iostream>
#include <valarray>
#include <cmath>


int main()
{
std::valarray<int> v = {1,2,3,4,5,6,7,8,9,10};
v = v.apply([](int n)->int {
return std::round(std::tgamma(n+1));
});
for(auto n : v) {
std::cout << n << ' ';
}
std::cout << '\n';
}


1 2 6 24 120 720 5040 40320 362880 3628800


for_each applies a function to a range of elements
(function template)
ranges::for_each applies a function to a range of elements
(C++20) (niebloid)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.