STL auto-sort function

Discussion in 'C++' started by Pat, May 18, 2004.

  1. Pat

    Pat Guest

    class data
    {
    public:
    double time;
    // other functions....
    }

    I want to put a lot of "data" classes into a container, and the data order
    should be sorted according to "time" variable. One of method is to use
    vector<data>. After push_back(), call sort function.
    Does there exist a container which provides "auto" sorting function? i.e.
    After inserting each data, I do not need to call implicitly "sort" for
    sorting.

    Thanks. Pat
    Pat, May 18, 2004
    #1
    1. Advertising

  2. "Pat" <> wrote in message news:40a9c7b0$-cable.com...
    > class data
    > {
    > public:
    > double time;
    > // other functions....
    > }
    >
    > I want to put a lot of "data" classes into a container, and the data order
    > should be sorted according to "time" variable. One of method is to use
    > vector<data>. After push_back(), call sort function.
    > Does there exist a container which provides "auto" sorting function? i.e.
    > After inserting each data, I do not need to call implicitly "sort" for
    > sorting.
    >
    > Thanks. Pat
    >


    Yes, std::set does that. Should be a lot more efficient that adding to a
    vector and sorting each time.

    john
    John Harrison, May 18, 2004
    #2
    1. Advertising

  3. Pat

    Siemel Naran Guest

    "Pat" <> wrote in message news:40a9c7b0$-cable.com...

    > class data
    > {
    > public:
    > double time;
    > // other functions....
    > }
    >
    > I want to put a lot of "data" classes into a container, and the data order
    > should be sorted according to "time" variable. One of method is to use
    > vector<data>. After push_back(), call sort function.
    > Does there exist a container which provides "auto" sorting function? i.e.
    > After inserting each data, I do not need to call implicitly "sort" for
    > sorting.


    Could 2 elements have the same time?
    Siemel Naran, May 18, 2004
    #3
  4. Pat

    Pat Guest

    Hi John,

    Could you provide a simple code example?

    Thanks.
    Pat
    "Siemel Naran" <> ¦b¶l¥ó
    news:pWjqc.18610$ ¤¤¼¶¼g...
    > "Pat" <> wrote in message news:40a9c7b0$-cable.com...
    >
    > > class data
    > > {
    > > public:
    > > double time;
    > > // other functions....
    > > }
    > >
    > > I want to put a lot of "data" classes into a container, and the data

    order
    > > should be sorted according to "time" variable. One of method is to use
    > > vector<data>. After push_back(), call sort function.
    > > Does there exist a container which provides "auto" sorting function?

    i.e.
    > > After inserting each data, I do not need to call implicitly "sort" for
    > > sorting.

    >
    > Could 2 elements have the same time?
    >
    >
    Pat, May 18, 2004
    #4
  5. Pat wrote:
    >
    > Hi John,
    >
    > Could you provide a simple code example?


    #include <iostream>
    #include <set>

    class data
    {
    public:
    bool operator < ( const data& arg ) const
    {
    return m_b < arg.m_b;
    }

    data( int a, int b ) : m_a( a ), m_b( b ) {}
    int a() { return m_a; }
    int b() { return m_b; }

    private:
    int m_a;
    int m_b;
    };

    typedef std::set< data > SetData;

    int main()
    {
    SetData Set;

    Set.insert( data( 5, 8 ) );
    Set.insert( data( 7, 3 ) );

    for( SetData::iterator i = Set.begin(); i != Set.end(); ++i )
    std::cout << i->a() << " " << i->b() << std::endl;

    return 0;
    }

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, May 18, 2004
    #5
  6. "Pat" <> wrote in message news:40a9ced0$-cable.com...
    > Hi John,
    >
    > Could you provide a simple code example?
    >
    > Thanks.
    > Pat


    #include <set>

    // to add data
    set<data> mySet;
    data someData = ...;
    mySet.insert(someData);

    // to loop through all data
    for (set<data>::const_iterator i = mySet.begin(); i != mySet.end(); ++i)
    {
    data d = *i;
    ...
    }

    As Siemel said the issue of whether you can have two data elements with the
    same time is an important one. If this is the case then you should use
    multiset not set.

    john
    John Harrison, May 18, 2004
    #6
  7. Pat

    P.J. Plauger Guest

    "John Harrison" <> wrote in message
    news:...

    > "Pat" <> wrote in message news:40a9c7b0$-cable.com...
    > > class data
    > > {
    > > public:
    > > double time;
    > > // other functions....
    > > }
    > >
    > > I want to put a lot of "data" classes into a container, and the data

    order
    > > should be sorted according to "time" variable. One of method is to use
    > > vector<data>. After push_back(), call sort function.
    > > Does there exist a container which provides "auto" sorting function?

    i.e.
    > > After inserting each data, I do not need to call implicitly "sort" for
    > > sorting.
    > >
    > > Thanks. Pat
    > >

    >
    > Yes, std::set does that. Should be a lot more efficient that adding to a
    > vector and sorting each time.


    Right. Or you could use vector and push_heap. You can probably
    trick priority_queue into doing the work for you -- assuming
    you just want to access the data once sequentially in time order
    (forward or reverse).

    P.J. Plauger
    Dinkumware, Ltd.
    http://www.dinkumware.com
    P.J. Plauger, May 18, 2004
    #7
  8. Pat

    Pat Guest

    Thanks all of you.

    Pat

    "Pat" <> ¦b¶l¥ó news:40a9c7b0$-cable.com ¤¤¼¶¼g...
    > class data
    > {
    > public:
    > double time;
    > // other functions....
    > }
    >
    > I want to put a lot of "data" classes into a container, and the data order
    > should be sorted according to "time" variable. One of method is to use
    > vector<data>. After push_back(), call sort function.
    > Does there exist a container which provides "auto" sorting function? i.e.
    > After inserting each data, I do not need to call implicitly "sort" for
    > sorting.
    >
    > Thanks. Pat
    >
    >
    Pat, May 18, 2004
    #8
    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. Rachel Forder
    Replies:
    2
    Views:
    371
    Rachel Forder
    Oct 12, 2003
  2. =?iso-8859-1?Q?=22Orlando_D=F6hring=22?=

    [B,IX] = sort(A,...) - Order for sort()-function

    =?iso-8859-1?Q?=22Orlando_D=F6hring=22?=, May 29, 2007, in forum: Python
    Replies:
    0
    Views:
    320
    =?iso-8859-1?Q?=22Orlando_D=F6hring=22?=
    May 29, 2007
  3. linkswanted
    Replies:
    1
    Views:
    901
  4. Navin
    Replies:
    1
    Views:
    688
    Ken Schaefer
    Sep 9, 2003
  5. GIMME
    Replies:
    5
    Views:
    185
    Thomas 'PointedEars' Lahn
    Jul 26, 2004
Loading...

Share This Page