c++11 标准模板(STL)(std::pair)(六)按字典序比较 pair 中的值

定义于头文件 <utility>

std::pair 是一个结构体模板,其可于一个单元存储两个相异对象。 pair 是 std::tuple 的拥有两个元素的特殊情况。

按字典序比较 pair 中的值

operator==,!=,<,<=,>,>=(std::pair)

template< class T1, class T2 >
bool operator==( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs );

(1) (C++14 前)

template< class T1, class T2 >
constexpr bool operator==( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs );

(1) (C++14 起)

template< class T1, class T2 >
bool operator!=( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs );

(2) (C++14 前)

template< class T1, class T2 >
constexpr bool operator!=( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs );

(2) (C++14 起)

template< class T1, class T2 >
bool operator<( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs );

(3) (C++14 前)

template< class T1, class T2 >
constexpr bool operator<( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs );

(3) (C++14 起)

template< class T1, class T2 >
bool operator<=( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs );

(4) (C++14 前)

template< class T1, class T2 >
constexpr bool operator<=( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs );

(4) (C++14 起)

template< class T1, class T2 >
bool operator>( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs );

(5) (C++14 前)

template< class T1, class T2 >
constexpr bool operator>( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs );

(5) (C++14 起)

template< class T1, class T2 >
bool operator>=( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs );

(6) (C++14 前)

template< class T1, class T2 >
constexpr bool operator>=( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs );

(6) (C++14 起)

1-2) 测试 lhs 和 rhs 的两个元素是否均相等,即比较 lhs.firstrhs.firstlhs.secondrhs.second

3-6) 按字典序比较 lhsrhs ,即比较首元素,然后仅若它们相等,再比较第二元素。

参数

lhs, rhs - 要比较的 pair

返回值

1) 若 lhs.first == rhs.firstlhs.second == rhs.second 则为 true ,否则为 false

2) !(lhs == rhs)

3) 若 lhs.first<rhs.first 则返回 true 。否则,若 rhs.first<lhs.first 则返回 false 。否则,若 lhs.second<rhs.second 则返回 true 。否则返回 false 。

4) !(rhs < lhs)

5) rhs < lhs

6) !(lhs < rhs)

调用示例

#include <iostream>
#include <string>
#include <iomanip>
#include <complex>
#include <tuple>
#include <typeinfo>

struct Cell
{
    int x;
    int y;

    Cell() = default;
    Cell(int a, int b): x(a), y(b) {}
    bool operator ==(const Cell &cell) const
    {
        return x == cell.x && y == cell.y;
    }

    bool operator <(const Cell &cell) const
    {
        if (x < cell.x)
        {
            return true;
        }
        return y < cell.y;
    }
};

std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
    os << "{" << cell.x << "," << cell.y << "}";
    return os;
}

std::ostream &operator<<(std::ostream &os, const std::pair<int, Cell> &pair)
{
    os << "pair{" << pair.first << " {" << pair.second.x << "," << pair.second.y << "}}";
    return os;
}

int main()
{

    //2) 以 x 初始化 first 并以 y 初始化 second 。
    std::pair<int, Cell> pair1(101, Cell(102, 103));
    std::pair<int, Cell> pair2(101, Cell(102, 103));
    std::pair<int, Cell> pair3(201, Cell(202, 203));

    std::cout << std::boolalpha;
    //1-2) 测试 lhs 和 rhs 的两个元素是否均相等,
    //即比较 lhs.first 和 rhs.first 及 lhs.second 和 rhs.second
    //3-6) 按字典序比较 lhs 和 rhs ,即比较首元素,然后仅若它们相等,再比较第二元素。

    //1) 若 lhs.first == rhs.first 且 lhs.second == rhs.second 则为 true ,否则为 false
    std::cout << pair1 << " == " << pair2 << ":     " << (pair1 == pair2) << std::endl;
    std::cout << pair1 << " == " << pair3 << ":     " << (pair1 == pair3) << std::endl;
    //2) !(lhs == rhs)
    std::cout << pair1 << " != " << pair2 << ":     " << (pair1 != pair2) << std::endl;
    std::cout << pair1 << " != " << pair3 << ":     " << (pair1 != pair3) << std::endl;
    //3) 若 lhs.first<rhs.first 则返回 true 。否则,若 rhs.first<lhs.first 则返回 false 。
    //否则,若 lhs.second<rhs.second 则返回 true 。否则返回 false 。
    std::cout << pair1 << " <  " << pair2 << ":     " << (pair1 <  pair2) << std::endl;
    std::cout << pair1 << " <  " << pair3 << ":     " << (pair1 <  pair3) << std::endl;
    //4) (rhs < lhs) || (lhs == rhs)
    std::cout << pair1 << " <= " << pair2 << ":     " << (pair1 <= pair2) << std::endl;
    std::cout << pair1 << " <= " << pair3 << ":     " << (pair1 <= pair3) << std::endl;
    //5) !(rhs < lhs)
    std::cout << pair1 << " >  " << pair2 << ":     " << (pair1 >  pair2) << std::endl;
    std::cout << pair1 << " >  " << pair3 << ":     " << (pair1 >  pair3) << std::endl;
    //6) !(lhs < rhs) || (lhs == rhs)
    std::cout << pair1 << " >= " << pair2 << ":     " << (pair1 >= pair2) << std::endl;
    std::cout << pair1 << " >= " << pair3 << ":     " << (pair1 >= pair3) << std::endl;

    return 0;
}

输出

pair{101 {102,103}} == pair{101 {102,103}}:     true
pair{101 {102,103}} == pair{201 {202,203}}:     false
pair{101 {102,103}} != pair{101 {102,103}}:     false
pair{101 {102,103}} != pair{201 {202,203}}:     true
pair{101 {102,103}} <  pair{101 {102,103}}:     false
pair{101 {102,103}} <  pair{201 {202,203}}:     true
pair{101 {102,103}} <= pair{101 {102,103}}:     true
pair{101 {102,103}} <= pair{201 {202,203}}:     true
pair{101 {102,103}} >  pair{101 {102,103}}:     false
pair{101 {102,103}} >  pair{201 {202,203}}:     false
pair{101 {102,103}} >= pair{101 {102,103}}:     true
pair{101 {102,103}} >= pair{201 {202,203}}:     false

猜你喜欢

转载自blog.csdn.net/qq_40788199/article/details/135045213