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 36
|
#include <iostream>
#include <vector>
#include <algorithm>
template<class iter, class compare>
void bubble_sort(iter first, iter last, compare cmp) {
bool done = false;
while (!done) {
done = true;
for (auto it = first; it != last - 1; ++it) {
if (!cmp(*it, *(it + 1))) {
std::swap(*it, *(it + 1));
done = false;
}
}
}
}
template < class T >
bool comp(T a, T b) {
return a < b;
}
int main() {
std::vector<int> vec{ 9,8,7,6,5,4,3,2,1 };
bubble_sort(vec.begin(), vec.end(), comp); //why cant I use comp as argument?
for (int x : vec)
std::cout << x << ' ';
return 0;
}
|