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 28 29 30 31 32 33 34 35
|
#include <algorithm>
#include <iostream>
#include <vector>
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v);
int main()
{
std::vector<int> vec { 0, 5, 2, 9, 7, 6, 1, 3, 4, 8 };
size_t compCounter { };
std::sort(vec.begin(), vec.end(),
[&compCounter] (int a, int b) noexcept
{
++compCounter;
return a < b;
});
std::cout << "number of comparisons: " << compCounter << '\n';
std::cout << vec << '\n';
}
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v)
{
for (auto const& x : v)
{
os << x << ' ';
}
return os;
}
|