Back to questions
JuniorTheoryCommon
What does the program print?
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v{2, 3, 1, 3};
std::sort(v.begin(), v.end(), [](int a, int b) { return a > b; });
for (int x : v) {
std::cout << x << ' ';
}
}3 3 2 1
- Expecting
1 2 3 3: the comparator returnstruewhenamust stand beforeb, soa > bputs the larger numbers first. - Expecting
3 2 1:std::sortonly reorders the elements and keeps both3s; duplicates disappear in astd::set, not in a sort.
- Why must this comparator use
>and not>=? - How would you sort
vin descending order without writing a lambda?