STL Samples : <utility>
関係演算子
operator!=
operator<=
operator>
operator>=
namespace rel_ops {
template<class T> bool operator!=(const T&, const T&);
template<class T> bool operator> (const T&, const T&);
template<class T> bool operator<=(const T&, const T&);
template<class T> bool operator>=(const T&, const T&);
};
Tに比較演算==,<が定義されているとき、==から!=を、<から>,<=,>=を生成します。
#include <iostream> #include <utility> #include "foo.h" using namespace std; using namespace std::rel_ops; void std_rel_ops() { cout << "operator!=, <=, >, >=" << endl; Foo f1(1); Foo f2(2); cout << boolalpha; cout << f1 << " == " << f2 << "..." << (f1 == f2) << endl; cout << f1 << " != " << f2 << "..." << (f1 != f2) << endl; cout << f1 << " < " << f2 << "..." << (f1 < f2) << endl; cout << f1 << " <= " << f2 << "..." << (f1 <= f2) << endl; cout << f1 << " > " << f2 << "..." << (f1 > f2) << endl; cout << f1 << " >= " << f2 << "..." << (f1 >= f2) << endl; }
pair
pair
make_pair
template <class T1, class T2>
struct pair {
typedef T1 first_type;
typedef T2 second_type;
T1 first;
T2 second;
pair();
pair(const T1& x, const T2& y);
template<class U, class V> pair(const pair<U, V> &p);
};
型T1,T2をメンバとする"2つ組"です。2つのpairの大小関係はそれぞれのfirstの大小関係に従い、両方のfirstが等しければsecondの大小関係に従います。
template <class T1, class T2> pair<T1,T2> make_pair(const T1& x, const T2& y);
pair<T1,T2>(x,y)を返します。
#include <iostream> #include <utility> #include "foo.h" #include "bar.h" using namespace std; void std_pair() { cout << "pair, make_pair" << endl; pair<Foo,Bar> p(1,2); cout << "pair<Foo,Bar>: [" << p.first << "," << p.second << ']' << endl; p = make_pair(Foo(3), Bar(4)); cout << "pair<Foo,Bar>: [" << p.first << "," << p.second << ']' << endl; }