Sorting Data Using a Lambda Expression - C++ STL

C++ examples for STL:Lambda

Description

Sorting Data Using a Lambda Expression

Demo Code

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

void StandardSort(vector<int>& vect)
{
    sort(vect.begin(), vect.end());/*from   w w w.  ja  v a  2s. co m*/
    cout << "Using the Standard Sort" << endl;
    for_each(vect.begin(), vect.end(), [](int x){cout << x << endl;});
}

void ProcessVector(vector<int>& vect)
{
    sort(vect.begin(), vect.end(), [](int S1, int S2){return S1 > S2;});
    cout << "Using a Custom Sort" << endl;
    for_each(vect.begin(), vect.end(), [](int x){cout << x << endl;});
}

int main()
{
    vector<int> myV;
    myV.push_back(11);
    myV.push_back(2);
    myV.push_back(33);
    myV.push_back(4);

    StandardSort(myV);
    ProcessVector(myV);
}

Result


Related Tutorials