#include<iostream>
#include <queue>
#include <iterator>
using namespace std;
class Myarray{
public:
typedef double LEN;
LEN Getlength(){
return 1.23012345;
}
};
//typename的两种意义
/*
1.表示模板类型的关键字 和class意义相同 但是不像class那样容易和类混淆
2.告诉c++编译器 typename后面的是类型 例如下面的函数 T是类 T:: 表示类的成员 其中LEN为T类中的一个类型。
*/
template< typename T >
void Myethod( T& myarr ){
typedef typename T::LEN lEN;
lEN length = myarr.Getlength();
cout << length << endl;
}
int main()
{
int n;
node a;
Myarray m;
Myethod<Myarray>(m);
return 0;
}
输出结果:
由于默认cout只会打印6位
想要显示完整的小数的方法:
1. cout << setiosflags(ios::fixed) <<setprecision(x) << length << endl;
如果length不足x位 后面补0
setiosflags在库<iomanip>里面
2. cout.setprecision(x)
这个函数如果x>length的长度 只会显示实际的小数部分 不会显示0