-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortStructstest.cpp
More file actions
32 lines (26 loc) · 806 Bytes
/
sortStructstest.cpp
File metadata and controls
32 lines (26 loc) · 806 Bytes
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
#include<bits/stdc++.h>
using namespace std;
struct matter {
int weight;
int value;
};
//compares two elements based on their value per unit weight.
bool compareByWorth(const matter& a, const matter& b)
{
if(a.weight == b.weight)
return a.value > b.value;
else if(a.value == b.value)
return a.weight < b.weight;
return (float)a.value/a.weight > (float)b.value/b.weight;
}
int main() {
vector<struct matter> element;
element.push_back({11,2});
element.push_back({20,5});
sort(element.begin(), element.end(), compareByWorth);
cout<<"sorted vector of structs in descending order of their value per unit weight -"<<endl;
for(int i=0;i<element.size();i++) {
cout<<element[i].value<<" "<<element[i].weight<<endl;
}
return 0;
}