valarray 类简介
valarray类是由头文件valarray支持的.
valarray是一个模板类
使用valarray类声明一个对象时, 需要在标识符valarray后面加上<>, 并在其中包含所需的数据类型:
valarray<int> q_values;
valarray<double> weights;
其常用的语法如下:
double gpa[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
// double类型数据的集合, size为0
valarray<double> v1;
// 含有8个int型数据的集合
valarray<int> v2(8);
// 含有8个int型数据的集合, 每个元素初始化为10
valarray<int> v3(10, 8);
// 含有四个double元素的集合, 并且使用gpa的前四个元素进行初始化
valarray<double> v4(gpa, 4);
// 还可以指定集合中的各个元素, 也就是使用初始化列表:
valarray<int> v5 = {1, 2, 3, 4, 5};
下面是valarray类常用的一些方法
operator[](): 能够访问各个元素
size(): 返回包含的元素数
sum(): 返回所有元素的总和
max(): 返回最大的元素
min(): 返回最小的元素.
看一个应用的例子:
//studentc.h
#ifndef STUDENTC_H_
#define STUDENTC_H_
#include <iostream>
#include <string>
#include <valarray>
class Student
{
private:
// 定义别名
typedef std::valarray<double> ArrayDb;
std::string name;
ArrayDb scores;
std::ostream & arr_out(std::ostream & os) const;
public:
Student(): name("Default"), scores() {}
// 防止隐式转换
explicit Student(const std::string & s) : name(s), scores(){};
explicit Student(int n) : name("Default"), scores(n) {}
Student(const std::string & s, int n): name(s), scores(n){}
Student(const std::string & s, const ArrayDb & a): name(s), scores(a){}
Student(const char * str, const double *pd, int n): name(str), scores(pd, n){}
// 析构函数
~Student() {}
double Average() const;
const std::string & Name() const;
double & operator[](int i);
double operator[](int i)const;
// 友元函数
friend std::istream & operator>>(std::istream & is, Student & stu);
friend std::istream & getline(std::istream & is, Student & stu);
friend std::ostream & operator<<(std::ostream & os, Student & stu);
};
#endif
第二个文件
// studentc.cpp
#include "student.h"
using std::ostream;
using std::endl;
using std::istream;
using std::string;
double Student::Average() const
{
if(scores.size() > 0)
return scores.sum() / scores.size();
else
return 0;
}
const string & Student::Name() const
{
return name;
}
double & Student::operator[](int i)
{
// 使用的是 valarray<double>::operator[]()方法
return scores[i];
}
double Student::operator[](int i) const
{
return scores[i];
}
ostream & Student::arr_out(ostream & os) const
{
int i;
int lim = scores.size();
if(lim > 0)
{
for(i = 0; i < lim; i++)
{
os << scores[i] << " ";
if(i % 5 == 4)
os << endl;
}
if(i % 5 != 0)
os << endl;
} else {
os << " empty array ";
}
return os;
}
// 友元函数
istream & operator>>(istream & is, Student & stu)
{
// 调用的是string的operator>>()
is >> stu.name;
return is;
}
istream & getline(istream & is, Student & stu)
{
// 使用的是string的友元getline(istream &, const string &)
getline(is, stu.name);
return is;
}
ostream & operator<<(ostream & os, Student & stu)
{
os << "Scores for " << stu.name << ": " << endl;
// 使用的是私有方法arr_out
stu.arr_out(os);
return os;
}
第三个文件:
// use_stuc.cpp
// compile with studentc.cpp
#include <iostream>
#include "student.h"
using std::cin;
using std::cout;
using std::endl;
void set(Student & sa, int n);
const int pupils = 3;
const int quizzes = 5;
int main()
{
// 使用quizzes初始化Student, 然后再初始化一个ada数组里面是三个Student对象
Student ada[pupils] = {Student(quizzes), Student(quizzes), Student(quizzes)};
int i;
for(i = 0; i < pupils; i++)
set(ada[i], quizzes);
cout << "Student List : " << endl;
for(i = 0; i < pupils; i++)
cout << ada[i].Name() << endl;
cout << endl << "Results:";
for(i = 0; i < pupils; i++)
{
cout << endl << ada[i];
cout << "average: " << ada[i].Average() << endl;
}
cout << "Done" << endl;
return 0;
}
void set(Student & sa, int n)
{
cout << "Please enter the student's name : ";
getline(cin, sa);
cout << "Please enter " << n << " quiz scores: " << endl;
for(int i = 0; i < n; i++)
cin >> sa[i];
while(cin.get() != '\n')
continue;
}
程序运行结果为: