min_element not functioning properly

Hello all,

I'm writing a small recursive program. Here is the code:

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
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> coins;


int checkchange(int left) {
	vector<int> choices (coins.size());
	if (left == 0)
		return 0;
	else {
		int min;
		for (int i=0;i<coins.size();i++) {
			choices.at(i) = (1 + checkchange(left - coins.at(i)));
		}
		return min_element(choices.front(),choices.back());
	}

}



int main() {
	int N;
	cin >> N;
	for (int i=0;i<N;i++) {
		int c,m,temp,change;
		cin >> c >> m;
		for (int j=0;j<c;j++) {
			cin >> temp;
			coins.push_back(temp);
		}
		
		for (int j=0;j<m;j++) {
			cin >> temp;
			change = checkchange(temp);
			cout << change;
		}
	}
	return 0;
	
}


I'm constantly getting the following error:

In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/algorithm:62,
from burningcoins.cpp:3:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h: In function ‘_FIter std::min_element(_FIter, _FIter) [with _FIter = int]’:
burningcoins.cpp:19: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h:5998: error: invalid type argument of ‘unary *’
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h:5998: error: invalid type argument of ‘unary *

What am I doing wrong? I tried compiling with gcc and g++. Neither worked.
The function expects iterators, not elements.
front() and back() return the elements at the start and the end.

return min_element(choices.begin(),choices.end());

Or if you want to compare just the two elements, then use std::min() instead.

return min(choices.front(), choices.back());
Topic archived. No new replies allowed.