std::less
|
定義於頭文件
<functional>
|
||
| template< class T > struct less; |
(C++14 前) | |
| template< class T = void > struct less; |
(C++14 起) | |
進行比較的函數對象。調用類型 T 上的 operator< ,除非特化。
特化
std::less 的特化為任何指針類型產生嚴格全序,即使內建的 operator< 不如此。嚴格全序在 std::less 、 std::greater 、 std::less_equal 和 std::greater_equal 對該指針類型的特化間一致,且亦與對應的內建運算符( < 、 > 、 <= 及 >= )所強加的部分順序一致。
| 若特化 |
(C++14 起) |
| 標准庫提供
|
(C++14 起) |
成員類型
| 類型 | 定義 |
result_type(C++17 中棄用) |
bool |
first_argument_type(C++17 中棄用) |
T |
second_argument_type(C++17 中棄用) |
T |
成員函數
|
operator()
|
檢查第一參數是否小於第二個 (公開成員函數) |
std::less::operator()
|
bool operator()( const T& lhs, const T& rhs ) const;
|
(C++14 前) | |
|
constexpr bool operator()( const T& lhs, const T& rhs ) const;
|
(C++14 起) | |
檢查 lhs 是否小於 rhs 。
參數
| lhs, rhs | - | 要比較的值 |
返回值
若 lhs < rhs 則為 true ,否則為 false 。
異常
(無)
可能的實現
constexpr bool operator()(const T &lhs, const T &rhs) const { return lhs < rhs; }
|
示例
1 #include <functional> 2 #include <iostream> 3 4 template <typename A, typename B, typename U = std::less<int>> 5 bool f(A a, B b, U u = U()) 6 { 7 return u(a, b); 8 } 9 10 int main() 11 { 12 std::cout << std::boolalpha; 13 std::cout << f(5, 20) << '\n'; 14 std::cout << f(100, 10) << '\n'; 15 }
輸出:
true
false
以上是在網上查的std::less用法。實際使用時,比如
std::map<uint64_t,Eigen::Vector2i, std::less<uint64_t>
這里第三個參數的作用為,定義map中key值的排序規則,這里即為按<排序。
