1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| bool cmpInt(int a, int b) { return a < b; } class CmpInt { bool operator()(const int a, const int b) const { return a < b; } }; int main() { std::vector<int> items{4, 3, 1, 2}; std::sort(items.begin(), items.end(), cmpInt); std::sort(items.begin(), items.end(), CmpInt()); std::sort(items.begin(), items.end(), [](int a, int b) { return a < b; } ); return 0; } template <class RandomAccessIterator, class Compare> void sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp) { if (comp(*it1, *it2)) }
std::function<bool(int, int)> f1(cmpInt); std::function<bool(int, int)> f2(CmpInt); std::function<bool(int, int)> f3([](int a, int b) { return a < b; });
|