operator==,!=,<,<=,>,>=,<=>(std::tuple)
定義於標頭檔案 <tuple> |
||
template< class... TTypes, class... UTypes > bool operator==( const std::tuple<TTypes...>& lhs, |
(1) | (C++11 起) (C++14 起為 constexpr) |
template< class... TTypes, class... UTypes > bool operator!=( const std::tuple<TTypes...>& lhs, |
(2) | (C++11 起) (C++14 起為 constexpr) (C++20 前) |
template< class... TTypes, class... UTypes > bool operator<( const std::tuple<TTypes...>& lhs, |
(3) | (C++11 起) (C++14 起為 constexpr) (C++20 前) |
template< class... TTypes, class... UTypes > bool operator<=( const std::tuple<TTypes...>& lhs, |
(4) | (C++11 起) (C++14 起為 constexpr) (C++20 前) |
template< class... TTypes, class... UTypes > bool operator>( const std::tuple<TTypes...>& lhs, |
(5) | (C++11 起) (C++14 起為 constexpr) (C++20 前) |
template< class... TTypes, class... UTypes > bool operator>=( const std::tuple<TTypes...>& lhs, |
(6) | (C++11 起) (C++14 起為 constexpr) (C++20 前) |
template< class... TTypes, class... UTypes > constexpr std::common_comparison_category_t< |
(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< |
(9) | (C++23 起) |
如果 sizeof...(TTypes) 不等於 sizeof...(UTypes),或者 std::get<i>(lhs) == std::get<i>(rhs) 對於範圍
[ 0, sizeof...(Types)) 內的任何 i 不是有效表示式,則程式格式錯誤。 如果 std::get<i>(lhs) == std::get<i>(rhs) 的型別和值類別對於範圍 [ 0, sizeof...(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)) 對於範圍 [ 0, sizeof...(Types)) 中的每個 i 滿足 boolean-testable 概念時才參與過載決議。 |
(C++26 起) |
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;
...
- 對於空元組,返回 std::strong_ordering::equal。
- 對於非空元組,效果等價於
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));
tuple-like
物件。/* Elems */ 表示型別包 std::tuple_element_t<i, UTuple>,對於範圍 [
0,
std::tuple_size_v<UTuple>)
中的每個 i 按遞增順序排列。此過載只能透過 實參依賴查詢 找到。所有比較運算子都是短路求值的;它們不會訪問超出確定比較結果所需範圍的元組元素。
|
(C++20 起) |
目錄 |
[編輯] 引數
lhs, rhs | - | 要比較的元組 |
[編輯] 返回值
[
0,
sizeof...(Types))
內的所有 i,std::get<i>(lhs) == std::get<i>(rhs) 為 true,否則為 false。對於兩個空元組,返回 true。[編輯] 註解
關係運算符是根據每個元素的 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 中的值(函式模板) |