我使用这个摸板类的程序
-
- #include <iostream>
- #include <utility>
- using namespace std;
- int main(){
- pair<int,int> a;
- a=make_pair(3,5);
- pair<int,int> b(3,8);
- pair<int,int> c(b);
- cout<<a.first<<" "<<a.second<<endl;
- cout<<b.first<<" "<<b.second<<endl;
- cout<<c.first<<" "<<c.second<<endl;
- cout<<(a==b)<<endl;
- cout<<(a!=b)<<endl;
- cout<<(a<b)<<endl;
- return 0;
- }
- //utility standard header
- #ifndef _UTILITY_
- #define _UTILITY_
- #include <iosfwd>
- namespace std {
- //TEMPLATE STRUCT pair
- template<class T1, class T2>
- struct pair //其实与类是一样的,只不过默认访问权限是public
- {
- typedef T1 first_type;
- typedef T2 second_type;
- pair():first(T1()),second(T2())//默认构造函数
- {
- }
- pair(const T1& V1,const T2 &V2)
- : first(V1),second(V2)
- {
- }
- template< class U1, class U2>//拷贝构造函数
- pair(const pair<U1, U2> & x)
- :first(X.first) ,second(X.second)
- {
- }
- T1 first;
- T2 second;
- };
- // pair TEMPLATE OPERATORS
- //注意operator==,operator<是实现其他的关键
- template<class T1, class T2>
- inline bool operator==(const pair<T1,T2>& X,
- const pair<T1,T2>& Y)
- {
- return (X.first==Y.first && X.second ==Y.second);
- }
- temlate<class T1, class T2>
- inline bool operator!=(const pair<T1,T2>& X,
- const pair<T1,T2>& Y)
- {
- return (!(X==Y));
- }
- temlate<class T1, class T2>
- inline bool operator<(const pair<T1,T2>& X,
- const pair<T1,T2>& Y)
- {
- return ( X.first <Y.first ||
- (!(Y.first<X.first) && X.second<Y.second ));
- }
- temlate<class T1, class T2>
- inline bool operator>(const pair<T1,T2>& X,
- const pair<T1,T2>& Y)
- {
- return (Y<X);
- }
- temlate<class T1, class T2>
- inline bool operator<=(const pair<T1,T2>& X,
- const pair<T1,T2>& Y)
- {
- return (!(Y<X));
- }
- temlate<class T1, class T2>
- inline bool operator>=(const pair<T1,T2>& X,
- const pair<T1,T2>& Y)
- {
- return (!( X<Y));
- }
- template<class T1, class T2>
- //注意 const修饰符不起作用
- inline pair<T1, T2> make_pair(const T1& X,const T2& Y)
- {
- return ( pair<T1, T2> (X, Y) );
- }
- //TEMPLATE OPERATORS
- namespace rel_ops
- {
- template<class T>
- inline bool operator!=(const T& X, cosnt T& Y)
- {
- return (!(X==Y) );
- }
- template<class T>
- inline bool operator>(const T& X, cosnt T& Y)
- {
- return (Y<X);
- }
- template<class T>
- inline bool operator<=(const T& X, cosnt T& Y)
- {
- return (!(Y<X) );
- }
- template<class T>
- inline bool operator>=(const T& X, cosnt T& Y)
- {
- return (!(X<Y) );
- }
- }
- }/* namespace std*/
- #endif /*UTILITY*/
测试这个类的文件
- //test <utility>
- #include <assert.h>
- #include <iostream>
- #include "utility.h"
- using namespace std;
- typedef pair<int, char> Pair_ic;
- Pair_ic p0;
- class Int
- {
- public:
- Int(int v):val(v)
- {
- }
- bool operator==(Int x) const
- {
- return (val==x.val);
- }
- bool operator<(Int x) const
- {
- return (val<x.val);
- }
- private:
- int val;
- };
- //TEST <utility>
- int main()
- {
- Pair_ic p1=p0;
- Pair_ic p2(3,'a');
- //TEST pair
- assert(p1.first==0);
- assert(p1.second==0);
- assert(p2.first==3);
- assert(p2.second=='a');
- assert(p2==make_pair( ( Pair_ic::first_type) 3,
- ( Pair_ic::second_type) 'a') );
- assert(p2 < make_pair( (Pair_ic::first_type) 4,
- (Pair_ic::second_type) 'b') );
- assert(p2 < make_pair( (Pair_ic::first_type) 3,
- (Pair_ic::second_type) 'b') );
- assert(p1 != p2);
- assert(p2 > p1);
- assert(p2 <=p2);
- assert(p2>=p2);
- //TEST rel_ops
- using namespace std::rel_ops;
- Int a(2), b(3);
- assert(a==a);
- // assert(a<b);
- assert(a != b);
- assert(b > a);
- assert(a <= b);
- assert(b >= a);
- cout<<"Success testing <utility>"<<endl;
- return 0;
- }