「c++/ライブラリ/ordered_pair」の編集履歴(バックアップ)一覧に戻る

c++/ライブラリ/ordered_pair - (2008/12/11 (木) 12:31:41) の編集履歴(バックアップ)


概要

firstのみで順序付けするpair。
(std::pairはfirst、second両方で順序付けする)

使い方

void ordered_pair_func()
{
	{
		std::pair<int, int> p1(10, 10), p2(20, 20), p3(10, 20);
 
		std::cout << "(10,10)<(10,10)=" << (p1<p1) << std::endl;	// false
		std::cout << "(10,10)>(10,10)=" << (p1>p1) << std::endl;	// false
		std::cout << "(10,10)<(20,20)=" << (p1<p2) << std::endl;	// true
		std::cout << "(10,10)>(20,20)=" << (p1>p2) << std::endl;	// false
		std::cout << "(10,10)<(10,20)=" << (p1<p3) << std::endl;	// true
		std::cout << "(10,10)>(10,20)=" << (p1>p3) << std::endl;	// false
	}
	std::cout << std::endl;
	{
		ordered_pair<int, int> p1(10, 10), p2(20, 20), p3(10, 20);
 
		std::cout << "(10,10)<(10,10)=" << (p1<p1) << std::endl;	// false
		std::cout << "(10,10)>(10,10)=" << (p1>p1) << std::endl;	// false
		std::cout << "(10,10)<(20,20)=" << (p1<p2) << std::endl;	// true
		std::cout << "(10,10)>(20,20)=" << (p1>p2) << std::endl;	// false
		std::cout << "(10,10)<(10,20)=" << (p1<p3) << std::endl;	// false
		std::cout << "(10,10)>(10,20)=" << (p1>p3) << std::endl;	// false
	}
}

コード

#ifndef UTIL_ORDERED_PAIR_H
#define UTIL_ORDERED_PAIR_H
 
#include <utility>
 
template<typename _Ty1, typename _Ty2>
struct ordered_pair : std::pair<_Ty1, _Ty2>
{
	ordered_pair() : std::pair<_Ty1, _Ty2>(){}
	ordered_pair(const _Ty1& _Val1, const _Ty2& _Val2) : std::pair<_Ty1, _Ty2>(_Val1, _Val2) {}
	template<typename _Other1, typename _Other2> ordered_pair(const ordered_pair<_Other1, _Other2>& _Right)
		: std::pair<_Ty1, _Ty2>(_Right)
	{}
};
 
 
template<typename _Ty1, typename _Ty2>
inline void swap(ordered_pair<_Ty1, _Ty2>& _Left, ordered_pair<_Ty1, _Ty2>& _Right)
{
	_Left.swap(_Right);
}
 
template<typename _Ty1, typename _Ty2>
inline bool operator==(const ordered_pair<_Ty1, _Ty2>& _Left, const ordered_pair<_Ty1, _Ty2>& _Right)
{
	return (_Left.first == _Right.first);
}
 
template<typename _Ty1, typename _Ty2>
inline bool operator!=(const ordered_pair<_Ty1, _Ty2>& _Left, const ordered_pair<_Ty1, _Ty2>& _Right)
{
	return (!(_Left == _Right));
}
 
template<typename _Ty1, typename _Ty2>
inline bool operator<(const ordered_pair<_Ty1, _Ty2>& _Left, const ordered_pair<_Ty1, _Ty2>& _Right)
{
	return (_Left.first < _Right.first);
}
 
template<typename _Ty1, typename _Ty2>
inline bool operator>(const ordered_pair<_Ty1, _Ty2>& _Left, const ordered_pair<_Ty1, _Ty2>& _Right)
{
	return (_Right < _Left);
}
 
template<typename _Ty1, typename _Ty2>
inline bool operator<=(const ordered_pair<_Ty1, _Ty2>& _Left, const ordered_pair<_Ty1, _Ty2>& _Right)
{
	return (!(_Right < _Left));
}
 
template<typename _Ty1, typename _Ty2>
inline bool operator>=(const ordered_pair<_Ty1, _Ty2>& _Left, const ordered_pair<_Ty1, _Ty2>& _Right)
{
	return (!(_Left < _Right));
}
template<typename _Ty1, typename _Ty2>
inline ordered_pair<_Ty1, _Ty2> make_pair(_Ty1 _Val1, _Ty2 _Val2)
{
	return (ordered_pair<_Ty1, _Ty2>(_Val1, _Val2));
}
 
} //namespace util
 
#endif
目安箱バナー