lexicographical sorting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tim.lino@gmail.com

    #1

    lexicographical sorting

    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.

  • Kai-Uwe Bux

    #2
    Re: lexicographical sorting

    tim.lino@gmail. com wrote:
    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,i nt>
    instead. Then, the appropriate overload for operator< would be defined
    already (and, yes, it does lexicographic order).


    Best

    Kai-Uwe Bux

    Comment

    Working...