https://leetcode.com/problems/move-zeroes/ was solving this question. But this doesnot compile.
void moveZeroes(vector<int>& nums) {
stable_sort(nums.begin(), nums.end(),[](int& a, int& b){
if(a == 0 && b != 0){
return false;
}
if(a != 0 && b == 0){
return true;
}
else {return false;}
});
}
However if we use pass by value that is
(int a, int b)
it compiles.
What is the underlying issue??
The lambda can take its arguments as refs. The problem is that in your example, they are not const-ref, and
std::stable_sort
passes const values,which cannot be converted to non-const ref. Simply declare the lambda to take
const int&
instead:The compiler error is a dead giveaway (filename truncated for brevity):