samedi 25 novembre 2017

Reverse deque content

I'm having a deque list of structs:

struct Position {
 Position(int _x, int _y) : x(_x), y(_y) {}

 int x;
 int y;
};

std::deque<Position> walkPath;

[...]

I would like to reverse walkPath. To do that I use reverse iterator:

std::deque<Position> reverseWalkPath;
for (auto it = walkPath.rbegin(); it != walkPath.rend(); it++) {
     reverseWalkPath.push_back(*it);
}
walkPath = reverseWalkPath; // make walkPath reverse

I would like to simplify this, not to create another deque variable.

Been trying to use:

std::reverse(walkPath.begin(), walkPath.end());

However, it was malforming the positions sometimes (random sequence of numbers instead of x, y).

So, is there a nice way to reverse order of deque elements?

Aucun commentaire:

Enregistrer un commentaire