名稱空間
變體
操作

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

來自 cppreference.com
< cpp‎ | 工具庫‎ | tuple
 
 
 
 
定義於標頭檔案 <tuple>
template< class... TTypes, class... UTypes >

bool operator==( const std::tuple<TTypes...>& lhs,

                 const std::tuple<UTypes...>& rhs );
(1) (C++11 起)
(C++14 起為 constexpr)
template< class... TTypes, class... UTypes >

bool operator!=( const std::tuple<TTypes...>& lhs,

                 const std::tuple<UTypes...>& rhs );
(2) (C++11 起)
(C++14 起為 constexpr)
(C++20 前)
template< class... TTypes, class... UTypes >

bool operator<( const std::tuple<TTypes...>& lhs,

                const std::tuple<UTypes...>& rhs );
(3) (C++11 起)
(C++14 起為 constexpr)
(C++20 前)
template< class... TTypes, class... UTypes >

bool operator<=( const std::tuple<TTypes...>& lhs,

                 const std::tuple<UTypes...>& rhs );
(4) (C++11 起)
(C++14 起為 constexpr)
(C++20 前)
template< class... TTypes, class... UTypes >

bool operator>( const std::tuple<TTypes...>& lhs,

                const std::tuple<UTypes...>& rhs );
(5) (C++11 起)
(C++14 起為 constexpr)
(C++20 前)
template< class... TTypes, class... UTypes >

bool operator>=( const std::tuple<TTypes...>& lhs,

                 const std::tuple<UTypes...>& rhs );
(6) (C++11 起)
(C++14 起為 constexpr)
(C++20 前)
template< class... TTypes, class... UTypes >

constexpr std::common_comparison_category_t<
    synth-three-way-result<TTypes, Elems>...>
    operator<=>( const std::tuple<TTypes...>& lhs,

                 const std::tuple<UTypes...>& rhs );
(7) (C++20 起)
template< class... TTypes, tuple-like UTuple >
constexpr bool operator==( const tuple<TTypes...>& lhs, const UTuple& rhs );
(8) (C++23 起)
template< class... TTypes, tuple-like UTuple >

constexpr std::common_comparison_category_t<
    synth-three-way-result<TTypes, /* Elems */>...>

    operator<=>( const tuple<TTypes...>& lhs, const UTuple& rhs );
(9) (C++23 起)
1,2) 透過 operator== 比較元組 lhs 的每個元素與元組 rhs 的對應元素。
1) 如果所有對應元素對都相等,則返回 true
2) 返回 !(lhs == rhs)
如果 sizeof...(TTypes) 不等於 sizeof...(UTypes),或者 std::get<i>(lhs) == std::get<i>(rhs) 對於範圍 [0sizeof...(Types)) 內的任何 i 不是有效表示式,則程式格式錯誤。
如果 std::get<i>(lhs) == std::get<i>(rhs) 的型別和值類別對於範圍 [0sizeof...(Types)) 內的任何 i 不滿足 BooleanTestable 要求,則行為未定義。
(直到 C++26)
此過載僅當 sizeof...(TTypes) 等於 sizeof...(UTypes)std::get<i>(lhs) == std::get<i>(rhs) 是有效表示式,且 decltype(std::get<i>(lhs) == std::get<i>(rhs)) 對於範圍 [0sizeof...(Types)) 中的每個 i 滿足 boolean-testable 概念時才參與過載決議。
(C++26 起)
3-6) 使用 operator< 按字典序比較 lhsrhs,即比較第一個元素,如果它們相等,則比較第二個元素,如果這些相等,則比較第三個元素,依此類推。
3) 對於空元組,返回 false。對於非空元組,效果等價於
if (std::get<0>(lhs) < std::get<0>(rhs)) return true;

if (std::get<0>(rhs) < std::get<0>(lhs)) return false;
if (std::get<1>(lhs) < std::get<1>(rhs)) return true;
if (std::get<1>(rhs) < std::get<1>(lhs)) return false;
...

return std::get<N - 1>(lhs) < std::get<N - 1>(rhs);
4) 返回 !(rhs < lhs)
5) 返回 rhs < lhs
6) 返回 !(lhs < rhs)
如果 sizeof...(TTypes) 不等於 sizeof...(UTypes),或者等價語句中顯示的任何比較表示式不是有效表示式,則程式格式錯誤。
如果等價語句中顯示的任何比較表示式的型別和值類別不滿足 BooleanTestable 要求,則行為未定義。
7) 透過 synth-three-way 按字典序比較 lhsrhs,即比較第一個元素,如果它們相等,則比較第二個元素,如果這些相等,則比較第三個元素,依此類推。

if (auto c = synth-three-way(std::get<0>(lhs), std::get<0>(rhs)); c != 0) return c;
if (auto c = synth-three-way(std::get<1>(lhs), std::get<1>(rhs)); c != 0) return c;
...
return synth-three-way(std::get<N - 1>(lhs), std::get<N - 1>(rhs));

8)(1),但 rhs 是一個 tuple-like 物件,且 rhs 的元素數量由 std::tuple_size_v<UTuple> 確定。此過載只能透過 實參依賴查詢 找到。
9)(7),但 rhs 是一個 tuple-like 物件。/* Elems */ 表示型別包 std::tuple_element_t<i, UTuple>,對於範圍 [0std::tuple_size_v<UTuple>) 中的每個 i 按遞增順序排列。此過載只能透過 實參依賴查詢 找到。

所有比較運算子都是短路求值的;它們不會訪問超出確定比較結果所需範圍的元組元素。

<, <=, >, >=, 和 != 運算子分別從 operator<=>operator== 合成

(C++20 起)

目錄

[編輯] 引數

lhs, rhs - 要比較的元組

[編輯] 返回值

1,8) 如果對於範圍 [0sizeof...(Types)) 內的所有 istd::get<i>(lhs) == std::get<i>(rhs)true,否則為 false。對於兩個空元組,返回 true
2) !(lhs == rhs)
3) 如果 lhs 中第一個不相等元素小於 rhs 中的對應元素,則返回 true;如果 rhs 中第一個不相等元素小於 lhs 中的對應元素,或者沒有不相等元素,則返回 false。對於兩個空元組,返回 false
4) !(rhs < lhs)
5) rhs < lhs
6) !(lhs < rhs)
7,9) 如果存在,則為第一對不相等元素之間的關係,否則為 std::strong_ordering::equal。對於兩個空元組,返回 std::strong_ordering::equal

[編輯] 註解

關係運算符是根據每個元素的 operator< 定義的。

(C++20 前)

關係運算符是根據 synth-three-way 定義的,它儘可能使用 operator<=>,否則使用 operator<

值得注意的是,如果元素型別本身不提供 operator<=>,但可以隱式轉換為三向可比較型別,則將使用該轉換而不是 operator<

(C++20 起)
特性測試 標準 特性
__cpp_lib_constrained_equality 202403L (C++26) std::tuple 的受限 operator==

[編輯] 示例

因為 operator< 為元組定義,所以元組容器可以排序。

#include <algorithm>
#include <iostream>
#include <tuple>
#include <vector>
 
int main()
{
    std::vector<std::tuple<int, std::string, float>> v
    {
        {2, "baz", -0.1},
        {2, "bar", 3.14},
        {1, "foo", 10.1},
        {2, "baz", -1.1},
    };
    std::sort(v.begin(), v.end());
 
    for (const auto& p: v)
        std::cout << "{ " << get<0>(p)
                  << ", " << get<1>(p)
                  << ", " << get<2>(p)
                  << " }\n";
}

輸出

{ 1, foo, 10.1 }
{ 2, bar, 3.14 }
{ 2, baz, -1.1 }
{ 2, baz, -0.1 }

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 2114
(P2167R3)
C++11 缺少布林操作的型別先決條件 已新增

[編輯] 參閱

(在 C++20 中移除)(在 C++20 中移除)(在 C++20 中移除)(在 C++20 中移除)(在 C++20 中移除)(C++20)
字典序比較 pair 中的值
(函式模板) [編輯]