c++ 好玩的类

http://www.cplusplus.com/reference/valarray/valarray/valarray/

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<int> 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<int> foo (init,5);

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

   for (size_t n=0; n<bar.size(); n++)

	  cout << bar[n] << ' ';

Output:
11 21 31 41 51

你可能感兴趣的:(类)