Max Element in a vector of structs

Hello. I have a function that takes a vector of employee structs and I need to find the max salary in the struct. I am trying to use the max_element function from the algorithm header but I get an obscene amount of errors with this code from line 34.
Any help would be great.

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

#include <string>
#include <iostream>
#include <vector>
#include <algorithm>


using namespace std;

struct employee
{
	string name;
	int id;
	double salary;

};

vector<employee>emps; // global vector

int main()
{
    .......	
    RemoveHighestSalary(emps);
    .....
    return 0;
}


void RemoveHighestSalary(vector<employee> &empIn)
{ 
	vector<employee>::iterator it;

	it = max_element (empIn.begin (), empIn.end ());
	double max = it->salary;
       
        cout << max;

}


Thanks.
Well, how is the compiler supposed to know what makes an employee "greater" than another?
See here:
http://www.cplusplus.com/reference/algorithm/max_element/

You'll need to implement operator< for your class or pass an appropriate functor to max_element.
Topic archived. No new replies allowed.