天天看点

了解utility

(1)基本概念

<utility>是一个很小的头文件,包含贯穿使用在STL中的几个模板的声明,在<utility>中剩下只有模板类pair,还有一些与之相关的模板函数和操作符,该模板类用来将两个对象表示成一个对象-----》当你想要一个函数返回两个值,或者想用一个容器来存储具有成对值的元素时,就非常方便

(2)基本函数

  • Pair     

可以构造一个带有明确初始值的模板类pair<T,U>

比如说获取x的第一个对象就用x.first , 获取x的第二个对象就用x.second

可以用另一个模板类pair<V,W>的对象来构造类pair<T,U>的对象

了解utility

T和U只需提供一个默认的构造函数,一个接受单参数的构造函数,一个析构函数

了解utility
  •  Make_pair(模板函数)

模板函数在检测模板参数时将忽略掉所有的const属性(!!!注意常量)

不能依赖make_pair来产生一个含有一个或者多个常量成员对象的pair

了解utility
  •   Operator==比较两个pair对象x,y如果x,y对应的成员都相等的话,就可以说x,y是相等的
    了解utility
  • Operator<
了解utility
  •  Operator!=
了解utility

该函数返回!(x == y)

  •  Operator>
  •  Operator<= 返回!(x>y)
  •  Operator>= 返回!(x<y)
了解utility

底下是模拟utility文件的做法

#ifndef UTILITY_
#define UTILITY_
namespace std{


template<class T1,class T2>
struct pair
{
	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;

};
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);
}
template<class T1, class T2>
inline bool operator!=(const pair<T1, T1>&x, const pair<T1, T2> &y)
{
	return !(x==y);

}
template<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));
}
template<class T1,class T2>
inline bool operator>(const pair<T1,T2>&x, const pair<T1,T2>&y)
{
	return (y<x);
}
template<class T1, class T2>
inline bool operator<=(const pair<T1, T2> &x, const pair<T1, T1> &y)
{
	return(!(y<x));
}
template<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>
inline pair<T1, T2>make_pair(const T1 &x, const T2 &y)
{
	return (pair<T1, T2>(x, y));
}

namespace rel_ops
{
	template<class T>
		inline bool operator==(const T&x, const T& y)
		{
			return x==y;
		}
	template<class T>//一个模板
		inline bool operator!=(const T& x, const T& y)
		{
			return!(x==y);
		}
	template<class T>
		inline bool operator<(const T& x, const T& y)
		{
			return x<y;
		}

	template<class T>
		inline bool operator>(const T&x, const T&y)
		{
			return y<x;
		}
	template<class T>
		inline bool operator>=(const T&x, const T&y)
		{
			return !(x<y);
		}
	template<class T>
		inline bool operator<=(const T&x, const T&y)
		{
			return (!y<x);
		}

}
}
#endif
           

测试文件:

#include<iostream>
using namespace std;
#include<utility>
#include<assert.h>

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;
		}
		bool operator>(Int x)const
		{
			return x<val;
		}
		bool operator!=(Int x)const
		{
			return !(x == *this);
		}
		bool operator>=(Int x)const
		{
			return !(*this<x);
		}
		bool operator<=(Int x)const
		{
			return !(*this > x);
		}
	private:
		int val;

};

int main()
{
	
	Pair_ic p1 = p0, p2(3, 'a');
	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 > p1);
	assert(p1 != p2);


	using namespace std::rel_ops;
	Int a(2), b(2);
	assert(a == b);
	assert(a >= b);
	assert(a <= b);
	cout<<"utility test successful"<<endl;
} 

           

结果:

了解utility