Get the maximun in a vector of struct

I want to get the maximun number of a variable which belong to a struct,so I have a vector composed by objects of that kind of struct,I want to analyse the variable order in all the objects through the vector composed by struct object and show the whole information of the one has the biggest number order...

here is my 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

struct bench{

		string name;
		int  stock;
		int order;
	};	
		
		
	bench CopyInformation(bench a){
		const string exit_cmd = "exit";
		string name;
		int order;
		int stock;
		while(name != exit_cmd) {
        cout << "Board name? ";
        getline(cin, name);
        if(!name.empty() && (name != exit_cmd)){
            cout<< "Board stock? ";
            cin >> stock;
            cout << "Board Order ";
            cin >> order;
            cin.ignore();
			a.name = name;
            a.stock= stock;
            a.order = order;
		
        }
        cout << "\n";
		}
		return a;	
	}
	
	bool SortByOrder(const bench& lhs, const bench& rhs){     
		return (lhs.order < rhs.order);
	}
	
	void FillWorkShop(vector<bench>& WorkShop){
		 bench newoperator;

    newoperator.name = "PCB185";
    newoperator.order = 152;
    newoperator.stock = 356;
    WorkShop.push_back(newoperator);

	newoperator.name = "PCB124";
    newoperator.order = 56;
    newoperator.stock = 23;
    WorkShop.push_back(newoperator);
	
	newoperator.name = "PCB500";
    newoperator.order = 456;
    newoperator.stock = 2265;
    WorkShop.push_back(newoperator);
	
	newoperator.name = "PCB326";
    newoperator.order = 7;
    newoperator.stock = 456;
    WorkShop.push_back(newoperator);
	
	newoperator.name = "PCB236";
    newoperator.order = 78;
    newoperator.stock = 99;
    WorkShop.push_back(newoperator);

   }
	
	
		
	
	
	void ShowBoards(const bench& s) {
    cout << left
         <<"Name: "  << s.name << ", "
         <<"Stock: "  << s.stock << ", "
         <<"Order: "  << s.order << "\n";
}
	

	

int main(int argc, char **argv)
{
	vector<bench> WorkShop;
	bench newoperator;
	newoperator = CopyInformation(newoperator);
	WorkShop.push_back(newoperator);
	FillWorkShop(WorkShop);
	sort(WorkShop.begin(),WorkShop.end(),SortByOrder);
	cout<< "Sort by Order Number"<< "\n";
	for(size_t i = 0; i != WorkShop.size();i++){
		ShowBoards(WorkShop[i]);
		}
		cout<<"\n";
	
	cout << "Board Information of Maximun Order"<<"\n";

	
	}



I have tryed with this:

http://www.cplusplus.com/reference/algorithm/max_element/


But I cant address it properly,because what I want to compare is a variable of a struct that belongs to a vector...

Does anyone know an idea?

Thanks!!
The second form of std::max_element takes a third parameter. All you need to do, is to provide that.

In the example there is an array of int. You have bench, not int.
I cant see the way to do it...

I'm doing this, but think It doesnt make any sense..
1
2
3
4
 int a = WorkShop.size();
	cout << "Board Information of Maximun Order"<<"\n";
	cout<<"Maximum Order:"<<"\n"<<*max_element(order.WorkShop,order.WorkShop+a,WorkShop);


The compiler says, that 'order' was not declared in the scope...

This is the example of Cplusplus
1
2
3
4
5
6
7
8
struct myclass {
  bool operator() (int i,int j) { return i<j; }
} myobj;

 // using object myobj as comp:
  std::cout << "The smallest element is " << *std::min_element(myints,myints+7,myobj) << '\n';
  std::cout << "The largest element is "  << *std::max_element(myints,myints+7,myobj) <<



I'm using the function where It takes an object.

In the third parameter I have to type the object in my case It's WorkShop which is an object of my Bench struct...and In the first and second parameter I have to put the first order belonging to the first struct and the last order belonging to the last struct.....I think the trouble is that WorkShop y a vector with this shape....Vector<Bench> WorkShop....

I dont know how to address this...could you explain me, please??I know there is somethin that I'm missing...
Last edited on
The myints in the example is an array. With vector you don't write WorkShop+7. You use end(WorkShop).

1
2
3
4
5
6
7
8
9
10
11
bool myfun( const bench & lhs, const bench & rhs )
{
  // return whatever is logical
};

int main() {
  vector<bench> WorkShop;
  // fill WorkShop
  std::cout << *std::max_element( std::begin(WorkShop), std::end(WorkShop), myfun );
  return 0;
}
yes, thats true It's a cointainer not an array...

Thanks!!
Topic archived. No new replies allowed.