Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > STL auto-sort function

Reply
Thread Tools

STL auto-sort function

 
 
Pat
Guest
Posts: n/a
 
      05-18-2004
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


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      05-18-2004

"Pat" <> wrote in message news:40a9c7b0$...
> 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


 
Reply With Quote
 
 
 
 
Siemel Naran
Guest
Posts: n/a
 
      05-18-2004
"Pat" <> wrote in message news:40a9c7b0$...

> 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?


 
Reply With Quote
 
Pat
Guest
Posts: n/a
 
      05-18-2004
Hi John,

Could you provide a simple code example?

Thanks.
Pat
"Siemel Naran" <> 在郵件
newsWjqc.18610$ 中撰寫...
> "Pat" <> wrote in message news:40a9c7b0$...
>
> > 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?
>
>



 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      05-18-2004
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

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      05-18-2004

"Pat" <> wrote in message news:40a9ced0$...
> 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


 
Reply With Quote
 
P.J. Plauger
Guest
Posts: n/a
 
      05-18-2004
"John Harrison" <> wrote in message
news:...

> "Pat" <> wrote in message news:40a9c7b0$...
> > 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


 
Reply With Quote
 
Pat
Guest
Posts: n/a
 
      05-18-2004
Thanks all of you.

Pat

"Pat" <> 在郵件 news:40a9c7b0$ 中撰寫...
> 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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation Fault on stl::resize / stl::clear Steve C++ 2 11-06-2007 06:53 AM
stl questions: how can I compare 2 stl list? silverburgh.meryl@gmail.com C++ 5 04-16-2006 09:57 PM
a stl map which use stl pair as the key Allerdyce.John@gmail.com C++ 2 02-22-2006 07:25 AM
Copy elements from one STL container to another STL container Marko.Cain.23@gmail.com C++ 4 02-16-2006 05:03 PM
To STL or not to STL Allan Bruce C++ 41 10-17-2003 08:21 PM



Advertisments