#include <iostream>
#include <array> // 必须包含!
#include <string> // 使用 string 需要这个头文件
using namespace std;
template <typename T>
using test=array<T,12>;
int main() {
//用法1,常规用法如下所示
typedef array<double,12> arrd;
typedef array<int,12> arri;
typedef array<string,12> arrst;
arrd gallons;
arri days;
arrst months;
for(int a=0;a<12;a++){
gallons[a]=a;
days[a]=a;
months[a]=to_string(a)+"test";//将int转为string字符串
}
for(int a=0;a<12;a++) {
cout<<"gallons["<<a<<"]="<<gallons[a]<<endl;
cout<<"days["<<a<<"]="<<days[a]<<endl;
cout<<"months["<<a<<"]="<<months[a]<<endl;
}
//用法2,采用模板定义数据类型,这个用法比较间接,如下所示:
test <int> aa;
test <double> bb;
test <string> cc;
for(int a=0;a<12;a++){
aa[a]=a;
bb[a]=a;
cc[a]=to_string(a)+"tx";//将int转为string字符串
}
for(int a=0;a<12;a++) {
cout<<"aa["<<a<<"]="<<aa[a]<<endl;
cout<<"bb["<<a<<"]="<<bb[a]<<endl;
cout<<"cc["<<a<<"]="<<cc[a]<<endl;
}
}
C++ Primer Plus 14.4.10 模板别名
最新推荐文章于 2025-12-17 21:45:35 发布
601

被折叠的 条评论
为什么被折叠?



