cant pass comparison function as argument

Hi, I was doing this one exercise where I have to implement a sorting algorithm but the problem is that I cant pass comparison function as argument. I know I could have used just < for it in my algorithm but I thought that this might be more fun to pass a function and I was right because now I might learn something new!

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;
}


If I was using bubble_sort(vec.begin(), vec.end(), [](int a, int b){return a < b;}); everything was working fine. Why it isnt when I'm trying to use comp function as last argument?
Any help appreciated
Last edited on
bubble_sort(vec.begin(), vec.end(), comp<int>);
ohh, thank you very much man :)
Topic archived. No new replies allowed.