STL auto-sort function

P

Pat

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
 
J

John Harrison

Pat said:
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
 
S

Siemel Naran

Pat said:
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?
 
K

Karl Heinz Buchegger

Pat said:
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;
}
 
J

John Harrison

Pat said:
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
 
P

P.J. Plauger

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
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top