程序示例:假设要求保存一年内四个季度的开支信息,并进行显示
#include <iostream>
#include <array>//array模板类头文件
#include <string>
using namespace std;
const int SEANUM=4;
const array<string,SEANUM>seasons={"Spring","Summer","Fall","Winter"};
void inExp(array<double,SEANUM>* expense);
void showExp(const array<double,SEANUM>* pt);
int main()
{
array<double,SEANUM>arr={0};
inExp(&arr);
showExp(&arr);
cout<<"Done.\n";
return 0;
}
//输入开支函数
void inExp(const array<double,SEANUM>* expense)
{
for (int i=0;i<SEANUM;i++)
{
cout<<seasons[i]<<" expense: ";
cin>>(*expense)[i];
}
}
//显示开支函数
void showExp( array<double,SEANUM>* pt)
{
for (int i=0;i<SEANUM;i++)
输出结果:
注意:
创建array对象需要包含头文件,名称空间std;
模板类并非只能存储基本数据类型,还能存储类对象。