std::less
來自 cppreference.com
定義於標頭檔案 <functional> |
||
template< class T > struct less; |
(until C++14) | |
template< class T = void > struct less; |
(C++14 起) | |
用於執行比較的函式物件。主模板在型別 T
上呼叫 operator<。
目錄 |
[編輯] 特化
(C++14) |
實現 x < y 的函式物件,推導引數和返回型別 (類模板特化) |
[編輯] 成員型別
型別 | 定義 |
result_type (C++17 中已棄用)(C++20 中已移除) |
bool |
first_argument_type (C++17 中已棄用)(C++20 中已移除) |
T
|
second_argument_type (C++17 中已棄用)(C++20 中已移除) |
T
|
這些成員型別透過公有繼承 std::binary_function<T, T, bool> 獲得。 |
(C++11 前) |
[編輯] 成員函式
operator() |
檢查第一個引數是否小於第二個引數 (公開成員函式) |
std::less::operator()
bool operator()( const T& lhs, const T& rhs ) const; |
(C++14 起為 constexpr) | |
檢查 lhs 是否小於 rhs。
引數
lhs, rhs | - | 要比較的值 |
返回值
lhs < rhs.
如果 T
是指標型別,則結果與 實現定義的指標全序 一致。
[編輯] 異常
可能丟擲實現定義的異常。
可能的實現
constexpr bool operator()(const T& lhs, const T& rhs) const { return lhs < rhs; // assumes that the implementation handles pointer total order } |
[編輯] 示例
執行此程式碼
#include <functional> template<typename A, typename B, typename C = std::less<>> constexpr bool fun(A a, B b, C cmp = C{}) { return cmp(a, b); } static_assert(fun(1, 2) == true); static_assert(fun(1.0, 1) == false); static_assert(fun(1, 2.0) == true); static_assert(std::less<int>{}(5, 5.6) == false); // 5 < 5 (warn: implicit conversion) static_assert(std::less<double>{}(5, 5.6) == true); // 5.0 < 5.6 static_assert(std::less<int>{}(5.6, 5.7) == false); // 5 < 5 (warn: implicit conversion) static_assert(std::less{}(5, 5.6) == true); // less<void>: 5.0 < 5.6 int main() {}
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 2562 | C++98 | 指標全序可能不一致 | 保證一致 |
[編輯] 參閱
實現 x == y 的函式物件 (類模板) | |
實現 x > y 的函式物件 (類模板) | |
(C++20) |
實現 x < y 的受限函式物件 (類) |