less_equal Struct
測試的二進位述詞之指定型別的值是否小於或等於該型別的另一個值。
template<class Type>
struct less_equal : public binary_function <Type, Type, bool>
{
bool operator()(
const Type& _Left,
const Type& _Right
) const;
};
參數
_Left
型別 [型別] 左運算元在要測試的不相等。_Right
型別 [型別] 右運算元在要測試的不相等。
傳回值
true ,如果 _Left <= _Right; false ,如果 _Left > _Right。
備註
這個二進位述詞 less_equal<[型別]>提供嚴格弱排定一組型別 [型別] 的元素值等價類別,則為;如果,才能使用這 [型別] 滿足有預先定義的標準數學需求。 任何指標型別 (Pointer Type) 的特製化會產生固定排程項目不同值的所有項目排序關於彼此。
範例
// functional_less_equal.cpp
// compile with: /EHsc
#define _CRT_RAND_S
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <functional>
#include <cstdlib>
#include <iostream>
int main( )
{
using namespace std;
vector <int> v1;
vector <int>::iterator Iter1;
vector <int>::reverse_iterator rIter1;
unsigned int randomNumber;
int i;
for ( i = 0 ; i < 5 ; i++ )
{
if ( rand_s( &randomNumber ) == 0 )
{
// Convert the random number to be between 1 - 50000
// This is done for readability purposes
randomNumber = ( unsigned int) ((double)randomNumber /
(double) UINT_MAX * 50000) + 1;
v1.push_back( randomNumber );
}
}
for ( i = 0 ; i < 3 ; i++ )
{
v1.push_back( 2836 );
}
cout << "Original vector v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
// To sort in ascending order,
// use the binary predicate less_equal<int>( )
sort( v1.begin( ), v1.end( ), less_equal<int>( ) );
cout << "Sorted vector v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
}
範例輸出
Original vector v1 = ( 31247 37154 48755 15251 6205 2836 2836 2836 )
Sorted vector v1 = ( 2836 2836 2836 6205 15251 31247 37154 48755 )
需求
標題: <functional>
命名空間: std