Answers for "sort using lambda c++"

C++
1

sort using lambda c++

vector<int> v = {50, -10, 20, -30};

sort(v.begin(), v.end());  // the default sort
// now v should be { -30, -10, 20, 50 }

// sort by absolute value:
sort(v.begin(), v.end(), [](int a, int b) { return abs(a)<abs(b); });
// now v should be { -10, 20, -30, 50 }
Posted by: Guest on May-12-2021

Browse Popular Code Answers by Language