How do I get the min or max element in a vector of objects in C ++, based on some field of the object?
I have this class:
class MyOwnClass
{
public:
int score; Specialcustomtype val1; double index;
private:
};
and the vector MyOwnClass
vector<MyOwnClass> MySuperVector(20);
Having some code that sets values ββto the fields of MyOwnClass, I want to find which MyOwnClass in the vector has the highest value field score.
In answer from related questions:
#include <algorithm> // For std::minmax_element
#include <tuple> // For std::tie
#include <vector> // For std::vector
#include <iterator> // For global begin() and end()
struct Size {
int width, height;
};
std::vector<Size> sizes = { {4, 1}, {2, 3}, {1, 2} };
decltype(sizes)::iterator minEl, maxEl;
std::tie(minEl, maxEl) = std::minmax_element(begin(sizes), end(sizes),
[] (Size const& s1, Size const& s2)
{
return s1.width < s2.width;
});
But in my case, the MyOwnClass fields are of different types and my attempts to use "max_elements" have failed.
Of course, I could loop through the n elements of the vector and use comparison to find which object has the highest score and it works, but I'm pretty sure C ++ built-in functions are more efficient in my version.
source to share
Try to run
std::vector<MyOwnClass> MySuperVector(20);
//..filling the vector
auto max = std::max_element( MySuperVector.begin(), MySuperVector.end(),
[]( const MyOwnClass &a, const MyOwnClass &b )
{
return a.score < b.score;
} );
If you need to find the minimum and maximum elements at the same time, you can use the standard algorithm std::minmax_element
. It returns a pair of iterators, the first pointing to the first minimum element and the second pointing to the last maximum element. Otherwise, you need to split std::max_element
and std::min_element
. if you need to get first low and first high or last low and last high
Another approach is to define internal functional objects for each field, which can be used to find the maximum or minimum. for example
class MyOwnClass
{
public:
int score; Specialcustomtype val1; double index;
struct ByScore
{
bool operator ()( const MyOwnClass &a, const MyOwnClass &b ) const
{
return a.score < b.score;
}
};
struct ByIndex
{
bool operator ()( const MyOwnClass &a, const MyOwnClass &b ) const
{
return a.index < b.index;
}
};
private:
};
//...
auto max_score = std::max_element( MySuperVector.begin(), MySuperVector.end(),
MyOwnClass::ByScore() );
auto max_index = std::max_element( MySuperVector.begin(), MySuperVector.end(),
MyOwnClass::ByIndex() );
source to share
It doesn't matter that your elements are of different types, you can always compare the field of interest:
std::vector<MyOwnClass> MySuperVector(20);
// A pred function to adjust according to your score
bool comparator(const MyOwnClass& s1, const MyOwnClass& s2) {
return s1.score < s2.score;
}
int main() {
MySuperVector[0].score = 23; // This will be returned
MySuperVector[1].score = 2;
MySuperVector[5].score = -22;
auto element = std::max_element(MySuperVector.begin(),
MySuperVector.end(), comparator);
std::cout << element->score; // 23
}
Note that you don't even need a function minmax_element
, as you are just asking for the largest element ( max_element is better suited).
If runtime efficiency matters to determine your highest value, you might want to take a look at priority_queues (heap adapters).
source to share
class test
{
private:
int num;
public:
test()
{
num = 0;
}
test(int n)
{
num = n;
}
void get(test ob1,test ob2)
{
if (ob1.num > ob2.num)
{
cout << "Max Object nu is:" << ob1.num << endl;
}
else
{
cout << "Max Object number is:" << ob2.num << endl;
}
}
};
void main()
{
test a(6);
test b(4);
test c;
c.get(a,b);
system("pause");
}
source to share