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

转载于:https://www.cnblogs.com/gisbeginner/archive/2012/12/30/2839637.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值