備忘録 @ ウィキ

ordered_pair

最終更新:

mlnk

- view
管理者のみ編集可

概要

firstのみで順序付けするpair。
(std::pairはfirst、second両方で順序付けする)
std::pairは非仮想デストラクタなんでポリモルフィックには扱えない。

使い方

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;	// 0
		std::cout << "(10,10)>(10,10)=" << (p1>p1) << std::endl;	// 0
		std::cout << "(10,10)<(20,20)=" << (p1<p2) << std::endl;	// 1
		std::cout << "(10,10)>(20,20)=" << (p1>p2) << std::endl;	// 0
		std::cout << "(10,10)<(10,20)=" << (p1<p3) << std::endl;	// 1
		std::cout << "(10,10)>(10,20)=" << (p1>p3) << std::endl;	// 0
	}
	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;	// 0
		std::cout << "(10,10)>(10,10)=" << (p1>p1) << std::endl;	// 0
		std::cout << "(10,10)<(20,20)=" << (p1<p2) << std::endl;	// 1
		std::cout << "(10,10)>(20,20)=" << (p1>p2) << std::endl;	// 0
		std::cout << "(10,10)<(10,20)=" << (p1<p3) << std::endl;	// 0
		std::cout << "(10,10)>(10,20)=" << (p1>p3) << std::endl;	// 0
	}
}

コード

#ifndef UTIL_ORDERED_PAIR_H
#define UTIL_ORDERED_PAIR_H
 
#include <utility>
 
namespace util {
 
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 std::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_ordered_pair(_Ty1 _Val1, _Ty2 _Val2)
{
	return (ordered_pair<_Ty1, _Ty2>(_Val1, _Val2));
}
 
} //namespace util
 
#endif
人気記事ランキング
目安箱バナー