033: operator<, operator<=, operator>, operator>=

operator<, operator<=, operator>, operator>= を定義します。

いわゆる辞書順で要素を比較していくグローバル関数を作成することになります。

template <typename T>
class myvector
{

...

};

/**
 * @brief      Compares the contents of lhs and rhs lexicographically.
 *             The comparison is performed by a function equivalent to
 *             std::lexicographical_compare.
 * @param[in]  lhs: Containers whose contents to compare in left-hand-side.
 * @param[in]  rhs: Containers whose contents to compare in right-hand-side.
 * @tparam     T: The type of the myvector elements. T must meet the requirements
 *                of LessThanComparable in order to use overloads. The ordering
 *                relation must establish total order.
 * @return     true if the contents of the lhs are lexicographically less than
 *             the contents of rhs, false otherwise.
 */
template <class T>
bool operator<(const myvector<T>& lhs, const myvector<T>& rhs)
{
    return (
        std::lexicographical_compare(
            lhs.begin(), lhs.end(), rhs.begin(), rhs.end()
        )
    );
}

/**
 * @brief      Compares the contents of lhs and rhs lexicographically.
 *             The comparison is performed by a function equivalent to
 *             std::lexicographical_compare.
 * @param[in]  lhs: Containers whose contents to compare in left-hand-side.
 * @param[in]  rhs: Containers whose contents to compare in right-hand-side.
 * @tparam     T: The type of the myvector elements. T must meet the requirements
 *                of LessThanComparable in order to use overloads. The ordering
 *                relation must establish total order.
 * @return     true if the contents of the lhs are lexicographically less than
 *             the contents of rhs, false otherwise.
 */
template <class T>
bool operator<=(const myvector<T>& lhs, const myvector<T>& rhs)
{
    return not (
        std::lexicographical_compare(
            rhs.begin(), rhs.end(), lhs.begin(), lhs.end()
        )
    );
}

/**
 * @brief      Compares the contents of lhs and rhs lexicographically.
 *             The comparison is performed by a function equivalent to
 *             std::lexicographical_compare.
 * @param[in]  lhs: Containers whose contents to compare in left-hand-side.
 * @param[in]  rhs: Containers whose contents to compare in right-hand-side.
 * @tparam     T: The type of the myvector elements. T must meet the requirements
 *                of LessThanComparable in order to use overloads. The ordering
 *                relation must establish total order.
 * @return     true if the contents of the lhs are lexicographically greater than
 *             the contents of rhs, false otherwise.
 */
template <class T>
bool operator>(const myvector<T>& lhs, const myvector<T>& rhs)
{
    return (
        std::lexicographical_compare(
            rhs.begin(), rhs.end(), lhs.begin(), lhs.end()
        )
    );
}

/**
 * @brief      Compares the contents of lhs and rhs lexicographically.
 *             The comparison is performed by a function equivalent to
 *             std::lexicographical_compare.
 * @param[in]  lhs: Containers whose contents to compare in left-hand-side.
 * @param[in]  rhs: Containers whose contents to compare in right-hand-side.
 * @tparam     T: The type of the myvector elements. T must meet the requirements
 *                of LessThanComparable in order to use overloads. The ordering
 *                relation must establish total order.
 * @return     true if the contents of the lhs are lexicographically less than
 *             the contents of rhs, false otherwise.
 */
template <class T>
bool operator>=(const myvector<T>& lhs, const myvector<T>& rhs)
{
    return not (
        std::lexicographical_compare(
            lhs.begin(), lhs.end(), rhs.begin(), rhs.end()
        )
    );
}
比較はすべて、std::lexicographical_compare() を使います。
operator< は、std::lexicographical_compare() をそのまま、
operator<= は、左辺と右辺を逆にした上で、not std::lexicographical_compare()、
operator> は、左辺と右辺を逆にした std::lexicographical_compare()、
operator>= は、not std::lexicographical_compare()、
をそれぞれ返すようにします。


全ソースコード: https://github.com/suomesta/myvector/tree/master/032

0 件のコメント:

コメントを投稿