lexicographical sorting

T

tim.lino

Dear all,

I would like to sort the elements in a vector in a lexicographical
order. The elements are defined as:

class E
{
public:
int x;
int y;
}

where x is the first rank and y is the second rank in the
lexicographical order. Suppose I have vector<E>, how I can sort E in a
lexicographical order? Is it any built-in function to do that?

Thank you.
 
K

Kai-Uwe Bux

I would like to sort the elements in a vector in a lexicographical
order. The elements are defined as:

class E
{
public:
int x;
int y;
}

where x is the first rank and y is the second rank in the
lexicographical order. Suppose I have vector<E>, how I can sort E in a
lexicographical order? Is it any built-in function to do that?

You need to (a) overload operator< for the type E or (b) specialize
std::less<> for the type E. Then, std::sort() will just sort any range for
you.

Alternatively, you could ditch the class E and just use std::pair<int,int>
instead. Then, the appropriate overload for operator< would be defined
already (and, yes, it does lexicographic order).


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top