less Struct
Visual Studio 2012
A binary predicate that tests whether a value of a specified type is less than another value of that type.
template<class Type>
struct less : public binary_function <Type, Type, bool>
{
bool operator()(
const Type& _Left,
const Type& _Right
) const;
};
The binary predicate less<Type> provides a strict weak ordering of a set of element values of type Type into equivalence classes if and only if this Type satisfies the standard mathematical requirements for being so ordered. The specializations for any pointer type yield a total ordering of elements in that all elements of distinct values are ordered with respect to each other.
// functional_less.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>
struct MyStruct {
MyStruct(int i) : m_i(i){}
bool operator < (const MyStruct & rhs) const {
return m_i < rhs.m_i;
}
int m_i;
};
int main() {
using namespace std;
vector <MyStruct> v1;
vector <MyStruct>::iterator Iter1;
vector <MyStruct>::reverse_iterator rIter1;
int i;
for ( i = 0 ; i < 7 ; i++ )
v1.push_back( MyStruct(rand()));
cout << "Original vector v1 = ( " ;
for ( Iter1 = v1.begin() ; Iter1 != v1.end() ; Iter1++ )
cout << Iter1->m_i << " ";
cout << ")" << endl;
// To sort in ascending order,
sort( v1.begin( ), v1.end( ), less<MyStruct>());
cout << "Sorted vector v1 = ( " ;
for ( Iter1 = v1.begin() ; Iter1 != v1.end() ; Iter1++ )
cout << Iter1->m_i << " ";
cout << ")" << endl;
}