By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,188 Members | 1,015 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,188 IT Pros & Developers. It's quick & easy.

STL auto-sort function

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a

"Pat" <Pa*@Pat.com> wrote in message news:40**********@rain.i-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
Jul 22 '05 #2

P: n/a
"Pat" <Pa*@Pat.com> wrote in message news:40**********@rain.i-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?
Jul 22 '05 #3

P: n/a
Pat
Hi John,

Could you provide a simple code example?

Thanks.
Pat
"Siemel Naran" <Si*********@REMOVE.att.net> 在郵件
news:PW*******************@bgtnsc04-news.ops.worldnet.att.net 中撰寫...
"Pat" <Pa*@Pat.com> wrote in message news:40**********@rain.i-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?

Jul 22 '05 #4

P: n/a

"Pat" <Pa*@Pat.com> wrote in message news:40**********@rain.i-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
Jul 22 '05 #5

P: n/a
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
kb******@gascad.at
Jul 22 '05 #6

P: n/a
"John Harrison" <jo*************@hotmail.com> wrote in message
news:2g************@uni-berlin.de...
"Pat" <Pa*@Pat.com> wrote in message news:40**********@rain.i-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
Jul 22 '05 #7

P: n/a
Pat
Thanks all of you.

Pat

"Pat" <Pa*@Pat.com> 在郵件 news:40**********@rain.i-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

Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.