lexicographical sorting

Discussion in 'C++' started by tim.lino@gmail.com, Mar 30, 2007.

  1. Guest

    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.
     
    , Mar 30, 2007
    #1
    1. Advertising

  2. Kai-Uwe Bux Guest

    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,int>
    instead. Then, the appropriate overload for operator< would be defined
    already (and, yes, it does lexicographic order).


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Mar 30, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edward C. Jones

    Lexicographical sort for numarray

    Edward C. Jones, Apr 27, 2005, in forum: Python
    Replies:
    5
    Views:
    400
    Jason Mobarak
    Apr 28, 2005
  2. Replies:
    2
    Views:
    1,489
    James Kanze
    Jul 6, 2010
  3. Jason
    Replies:
    0
    Views:
    411
    Jason
    Oct 4, 2006
  4. Tom Kirchner

    sorting by multiple criterias (sub-sorting)

    Tom Kirchner, Oct 11, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    510
    Michael Budash
    Oct 11, 2003
  5. Íéêüëáïò Êïýñáò

    Sorting a set works, sorting a dictionary fails ?

    Íéêüëáïò Êïýñáò, Jun 10, 2013, in forum: Python
    Replies:
    12
    Views:
    169
    Ulrich Eckhardt
    Jun 10, 2013
Loading...

Share This Page