473,414 Members | 1,776 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

STL auto-sort function

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

"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
"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
Pat
Hi John,

Could you provide a simple code example?

Thanks.
Pat
"Siemel Naran" <Si*********@REMOVE.att.net> ¦b¶l¥ó
news:PW*******************@bgtnsc04-news.ops.worldnet.att.net ¤¤¼¶¼g...
"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

"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
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
"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
Pat
Thanks all of you.

Pat

"Pat" <Pa*@Pat.com> ¦b¶l¥ó news:40**********@rain.i-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

Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Glabbeek | last post by:
I'm changing the layout of my site. Instead of using tables, I will use DIVs. It's working fine, except for 1 thing: In IE6 some DIVs are not the correct width. Mozilla and Opera are showing the...
5
by: Robert Downes | last post by:
I'm using the following in a page that I'm testing in Mozilla: p.actionLinkBlock {border: 1px #000000 dashed; padding: 0.2cm; width: auto} But the dashed border is extending to the right-edge...
20
by: Vijay Kumar R. Zanvar | last post by:
Hello, Unlike register, auto keyword can not be used to declare formal parameter(s). Is there any specific reason for this? Kind regards, Vijay Kumar R. Zanvar
6
by: Alpha | last post by:
I retrieve a table with only 2 columns. One is a auto-generated primary key column and the 2nd is a string. When I add a new row to the dataset to be updated back to the database. What should I...
5
by: Samuel | last post by:
Hi, I am running into a problem of mixing UICulture = auto and allowing users to select culture using a dropdown list. I am detecting a querystring, "setlang", and when found, setting the...
5
by: maya | last post by:
at work they decided to center divs thus: body {text-align:center} #content {width: 612px; text-align:left; margin: 0 auto 0 auto; } this works fine in IE & FF, EXCEPT in FF it doesn't work if...
13
by: S.Dickson | last post by:
I had an access database that i use as an ordering system. I have a form for entering customer details. When i add a new customer on the form the customer number is an auto number that appears when...
2
by: Piotr K | last post by:
Hi, I've encountered a strange problem with Firefox which I don't have any idea how to resolve. To the point: I've <divelement with a style "height: auto" and I want to retrieve this value...
1
by: neridaj | last post by:
Hello, I've found a few postings of this problem but none of the answers seem to fix my problem. I have a content div wrapped around a left floated image and a right floated table. I want the...
21
by: JOYCE | last post by:
Look the subject,that's my problem! I hope someone can help me, thanks
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.