Evan Ward
Evan Ward

Reputation: 1439

C++ - 2D Z-sorting across multiple vectors of objects

I'm trying to figure out a way to draw multiple vectors of objects in the right order visually. I understand how to sort the vector based on a point to make things look about right, but how can I do this across multiple vectors? Storing all of the different types of objects in one vector seems like a bad idea.

Upvotes: 0

Views: 66

Answers (1)

Mooing Duck
Mooing Duck

Reputation: 66961

Create a single vector that contains pointers to the elements in the other vectors, and then sort that vector (using a dereferencing comparitor of course)

template<class comparitor>
struct deref_comparitor {
    deref_comparitor(comparitor comp={}): comp(comp) {}
    template<class pointer>
    bool operator()(pointer left, pointer right) {return comp(*left, *right);}
private:
    comparitor comp;
};


std::vector<T*> thingies = {....};
std::sort(thingies.begin(), thingies.end(), deref_comparitor<std::less<T>>());

Hmm, I wonder if something like this is already in the standard library?

See it working here: http://coliru.stacked-crooked.com/a/8ed0015a7535b236

Upvotes: 0

Related Questions