- valarray[meta header]
- std[meta namespace]
- valarray[meta class]
- function[meta id-type]
ValOrProxy<T> apply(T func(T)) const;
ValOrProxy<T> apply(T func(const T&)) const;
- ValOrProxy[italic]
各要素に任意の関数を適用する。
*this
の全ての要素に関数func
を適用したvalarray
オブジェクトを新たに作って返す。
返されるvalarray
オブジェクトは、*this
と同じ要素数、同じ要素型を持つ。
- 戻り値の型
ValOrProxy
は、valarray
、あるいは、その代理となる型である。
<valarray>
の概要も参照のこと。
#include <iostream>
#include <valarray>
int main()
{
const std::valarray<int> va = {1, 2, 3};
// 全要素を+1する
std::valarray<int> result = va.apply([](int x) { return x + 1; });
for (int x : result) {
std::cout << x << std::endl;
}
}
- apply[color ff0000]
2
3
4