股票

valarray数组类型的构造方法

使用 valarray 定义可变数组有以下几种方法:

  1. double          gpa[5] = { 3.1, 3.5, 3.8, 2.9, 3.3 };
  2. valarray<double>    v1;             /* 浮点数组,大小为0  ——① */
  3. valarray<int>       v2( 8 );        /* 整型数组,大小为8   ——② */
  4. valarray<int>       v3( 10, 8 );    /* 整型数组,大小为8,每个值初始化为10   ——③ */
  5. valarray<double>    v4( gpa, 4 );   /* 浮点数组,大小为4,每个值初始化为数组 gpa 的前4个值   ——④ */
  6. valarray<int>       v5( v3 );       /* 整型数组,同v3   ——⑤ */

当作为类中的元素的时候,生成构造函数的方法:

  1. class Student  
  2. {  
  3. private:  
  4.     typedef std::valarray<double> ArrayDb;  
  5.     std::string name;  
  6.     ArrayDb scores;  
  7.     …  
  8.   
  9. public:  
  10.     Student() : name( “ Null Student ” ), scores()  
  11.     {}                                       /*  ——① */  
  12.     explicit Student( const std::string & s ) : name( s ), scores()  
  13.     {}                                       /*  ——① */  
  14.     explicit Student( int n ) : name( “ Nully ” ), scores( n )    
  15.     {}                                       /*   ——② */  
  16.     Student( const std::string & s, int n ) : name( s ), scores( n )  
  17.     {}                                       /*  ——② */  
  18.     Student( const std::string & s, const ArrayDb & a ) : name( s ), scores( a )  
  19.     {}                                       /*   ——⑤ */  
  20.     Student( const char * str, const double * pd, int n ) : name( str ), scores( pd, n )  
  21.     {}                                       /*  ——④ */  
  22.     …  
  23. };  
打赏
原文链接:,转发请注明来源!

发表评论