A
A
AiR_WiZArD2020-08-13 17:04:40
C++ / C#
AiR_WiZArD, 2020-08-13 17:04:40

How to sort a vector by one of the structure elements?

There is a structure that stores some parameters:

typedef struct
{
  sf::Vector2f position;			// Координаты
  float angle;					// Угол относительно оси
  float thrust;					// Тяга трастера
//... и так далее
} calcThrusterType;


a vector is composed and filled from this structure: How can a vector be sorted by one element? You need to get the structures in ascending order of the angle.
std::vector <calcThrusterType> thrusters;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mercury13, 2020-08-13
@AiR_WiZArD

std::sort(thrusters.begin(), thrusters.end(), 
    [](const calcThrusterType& x, const calcThrusterType& y) {
        return (x.angle < y.angle);
    });

Are you using typedef and your compiler doesn't have C++11?
bool angleLess(const cTT& x, const cTT& y) { return x.angle < y.angle; }

std::sort(thrusters.begin(), thrusters.end(), angleLess);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question