std::valarray::apply

来自cppreference.com

 
 
Numerics的图书馆
常见的数学函数
浮点环境
复数
数字阵列
伪随机数生成
编译时合理的算法 (C++11)
通用的数值运算
Original:
Generic numeric operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
iota(C++11)
accumulate
inner_product
adjacent_difference
partial_sum
 
std::valarray
成员函数
Original:
Member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
非成员函数
Original:
Non-member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
swap(C++11)
begin(C++11)
end(C++11)
abs
exp
log
log10
pow
sqrt
sin
cos
tan
asin
acos
atan
atan2
sinh
cosh
tanh
Helper类
Original:
Helper classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
 
valarray<T> apply( T func(T) ) const;
valarray<T> apply( T func(const T&) ) const;
通过施加到先前的值的元素的功能func购入的大小相同的值返回一个新的valarray.
Original:
Returns a new valarray of the same size with values which are acquired by applying function func to the previous values of the elements.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

目录

[编辑] 参数

func -
功能适用的值
Original:
function to apply to the values
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[编辑] 返回值

由此产生的valarray与应用功能func,获取的值.
Original:
The resulting valarray with values acquired by applying function func.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[编辑] 注释

该功能可以实现从std::valarray不同的返回类型。在这种情况下,替换类型具有以下属性:
Original:
The function can be implemented with the return type different from std::valarray. In this case, the replacement type has the following properties:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • const提供的所有std::valarray成员函数.
    Original:
    All const member functions of std::valarray are provided.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • std::valarraystd::slice_arraystd::gslice_arraystd::mask_arraystd::indirect_array可以由更换型.
    Original:
    std::valarray, std::slice_array, std::gslice_array, std::mask_array and std::indirect_array can be constructed from the replacement type.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 所有的函数接受一个参数的类型const std::valarray&也应接受替换类型.
    Original:
    All functions accepting a arguments of type const std::valarray& should also accept the replacement type.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 接受两个参数的类型const std::valarray&应该接受所有的功能const std::valarray&的每个组合和置换型.
    Original:
    All functions accepting two arguments of type const std::valarray& should accept every combination of const std::valarray& and the replacement type.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

[编辑] 为例

第10阶乘计算和打印
Original:
calculates and prints the first 10 factorials
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#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';
}

Output:

1 2 6 24 120 720 5040 40320 362880 3628800

[编辑] 另请参阅

某一范围的元素应用一个函数
Original:
applies a function to a range of elements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(函数模板) [edit]