A valarray object is designed to hold an array of elements, and easily perform mathematical operations on them.
// valarray constructor example #include <iostream> #include <valarray> using namespace std; int main () { int init[]= {10,20,30,40}; valarray<int> first; // (empty) valarray<int> second (5); // 0 0 0 0 0 valarray<int> third (10,3); // 10 10 10 valarray<int> fourth (init,4); // 10 20 30 40 valarray<int> fifth (fourth); // 10 20 30 40 cout << "fifth sums " << fifth.sum() << endl; return 0; }
Output: fifth sums 100
int init[]={10,20,30,40,50}; valarray myvalarray (init,5); // 10 20 30 40 50 myvalarray = myvalarray.cshift(2); // 30 40 50 10 20 myvalarray = myvalarray.cshift(-1); // 20 30 40 50 10
Returns a valarray with its elements rotated left n spaces (or right if n is negative).
每个元素都按照(1+n)%size() 这种方式旋转
int increment (int x) { return ++x;}
int init[]={10,20,30,40,50}; valarray foo (init,5); valarray bar = foo.apply(increment); for (size_t n=0; n
Output: 11 21 31 41 51