STL中有pair用于实现成对的元素,在OpenFOAM中叶实现了这一个模板类。template<class Type>Pair:public FixedList<Type,2>;//由此可见该模板类是FixedList的子类,且该FixedList容量为2,即只有两个元素,这里的Fixed是OpenFOAM的container模板类里的一个,以后再介绍container时再进行详细的介绍。该模板类的公有部分: 构造函数: Pair(){}//默认构造函数什么都不做 Pair(const Type& f, const Type& s)//给出pair对元素来进行构造 { first()=f; second()=s } //这里分别调用了类成员函数first()和second()来返回对两个元素的引用 Pair(Istream& is) //从输入流进行构造 :FixedList<Type,2>(is)//调用父类的相应构造函数完成工作 {} 成员函数: inline Type first() {return this->operator[](0);}//返回第一个元素进行操作 inline Type second() {return this->operator[](1);}//返回第二个元素 inline Type first() const{....}//first()的const版本 inline Type second() {...}//second()的const版本 inline Pair<Type> reversePair() const { return Pair<Type>(second(),first()); } //将第一个元素和第二个元素互换 友元操作符://用来比较两个Pair是否相等的 inline friend bool operator==(const Pair<Type>& a, const Pair<Type>& b); inline friend bool operator!=(const Pair<Type>& a, const Pair<Type>& b); 两个const的同类型(元素都是Type类型的)的Pair只有当两个元素完全相等时才是相等的,否则就是不相等的。 简单介绍了Pair!欢迎评论!

评论