「c++/ライブラリ/ordered_pair」の編集履歴(バックアップ)一覧はこちら
「c++/ライブラリ/ordered_pair」(2008/12/12 (金) 23:58:14) の最新版変更点
追加された行は緑色になります。
削除された行は赤色になります。
***概要
firstのみで順序付けするpair。~
(std::pairはfirst、second両方で順序付けする)
***使い方
#codehighlight(C++){{
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
}
}
}}
***コード
#codehighlight(C++){{
#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
}}
***概要
firstのみで順序付けするpair。~
(std::pairはfirst、second両方で順序付けする)~
std::pairは非仮想デストラクタなんでポリモルフィックには扱えない。
***使い方
#codehighlight(C++){{
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
}
}
}}
***コード
#codehighlight(C++){{
#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
}}
表示オプション
横に並べて表示:
変化行の前後のみ表示: