Thursday 14 June 2018

Set Comparator



#include 
#include 

struct C
{
    bool operator()(const int &a, const int &b) const
    {
        return a % 10 < b % 10;
    }
};

int main()
{
    std::set x({ 4, 2, 7, 11, 12, 14, 17, 2 });
    std::cout << x.size() << std::endl; // size is 7 ( 4,2,7,11,12,14,17) and duplicate elements are removed.
    std::set<int, C> y(x.begin(), x.end());// Operator Comparator
    std::cout << y.size() << std::endl;;// size if 4( 4,2,7,11) and duplicated elements are removed based on
comparision (i.e.  12%10 = 2 < 17%10....2 is already added in list std::set::iterator iter; for (iter = y.begin(); iter != y.end(); ++iter) { std::cout << *iter << std::endl;//;// 4,2,7 11 } return 0; }
           

No comments:

Post a Comment