Sorting a vector of structs
Dec 23, 2013 at 11:11am UTC
Hi,
in my C++ program i have a vector (lets name it "fooVector") which holds many elements of a stuct (lets name it "FooStruct").
1 2 3 4 5 6 7 8 9
struct FooStruct
{
int Position;
double ValueA;
double ValueB;
double ValueC;
};
vector<FooStruct> fooVector;
The FooStruct has an integer variable called "Position" and some other variables. Now i would like to sort my fooVector in order of the FooStruct->Position.
Maybe it is also important to know, that Position is not always unique, same positions could occur over several FooStructs.
Is this possible to do? Or do i have to use something other than a vector for such a use-case?
Please excuse my bad english.
Thanks for all replies in advance.
Regards,
Mathias
Dec 23, 2013 at 11:15am UTC
No, its easily possible. You can use algorithms std::sort, and pass it a functoid of what you want it to do:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <algorithm>
#include <vector>
// in some func
std::vector<FooStruct> fooVector;
// populate the vector
auto sortFunc = [](FooStruct a, FooStruct b) -> bool {
return a.Position < b.Position;
};
std::sort(fooVector.begin(), fooVector.end(), sortFunc);
Last edited on Dec 23, 2013 at 11:15am UTC
Dec 23, 2013 at 12:01pm UTC
Wow, i didnt expect that this can be done with so less code.
Thank you NT3!
Last edited on Dec 23, 2013 at 12:07pm UTC
Dec 23, 2013 at 12:07pm UTC
Or add an overloaded
<
operator to the struct or class.
1 2 3 4 5 6 7 8 9 10 11 12
struct FooStruct
{
int Position;
double ValueA;
double ValueB;
double ValueC;
bool operator <(const FooStruct & rhs) const
{
return (Position < rhs.Position);
}
};
then sorting is just:
std::sort(fooVector.begin(), fooVector.end());
Dec 23, 2013 at 9:21pm UTC
Ahh, thank you Chervil. Now it is clearer to me.
That is usually the way i do sorting in VB and C#. I didnt thought of operator overloading in C++.
Dec 23, 2013 at 10:05pm UTC
or you can write a static compare (by position) method for FooStruct:
1 2 3 4 5 6 7 8 9 10
struct FooStruct
{
int Position;
double ValueA;
double ValueB;
double ValueC;
static bool compareByPosition(const FooStruct& lhs, const FooStruct& rhs)
{ return lhs.Position < rhs.Position; }
};
std::sort(fooVector.begin(), fooVector.end(), FooStruct::compareByPosition);
this way you can write many comparers for each field in FooStruct
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
struct FooStruct
{
int Position;
double ValueA;
double ValueB;
double ValueC;
static bool compareByPosition(const FooStruct& lhs, const FooStruct& rhs)
{ return lhs.Position < rhs.Position; }
static bool compareByValueA(const FooStruct& lhs, const FooStruct& rhs)
{ return lhs.ValueA < rhs.ValueA; }
static bool compareByValueB(const FooStruct& lhs, const FooStruct& rhs)
{ return lhs.ValueB < rhs.ValueB; }
static bool compareByValueC(const FooStruct& lhs, const FooStruct& rhs)
{ return lhs.ValueC < rhs.ValueC; }
};
Last edited on Dec 23, 2013 at 10:06pm UTC
Dec 24, 2013 at 12:10am UTC
There's some useful advice here. Real-world problems can require sorting on multiple fields, perhaps with some ascending, others descending. Flexible approaches to sorting are definitely useful.
Topic archived. No new replies allowed.