sorting time_t

G

Gary Wessle

dear programmers
I am in need of some help please.
I am trying to sort vector<time_t> assending.
the following is by best go but needs some guide.

struct assending : public binary_function<time_t, time_t, bool> {
bool operator()(time_t x, time_t y) {
return x > y;
}
}


sort(time_vec.begin(), time_vec.end(), assending());

will this be the correct way to do it?

thanks
 
A

Alan Johnson

Gary said:
dear programmers
I am in need of some help please.
I am trying to sort vector<time_t> assending.
the following is by best go but needs some guide.

struct assending : public binary_function<time_t, time_t, bool> {
bool operator()(time_t x, time_t y) {
return x > y;
}
}


sort(time_vec.begin(), time_vec.end(), assending());

will this be the correct way to do it?

thanks

There is a standard version of your "assending" functor, called greater.
All you need to do is:

#include <algorithm>
// ...
sort(time_vec.begin(), time_vec.end(), greater<time_t>()) ;

See this link for details:
http://www.sgi.com/tech/stl/greater.html
 
A

Alan Johnson

Gary said:
dear programmers
I am in need of some help please.
I am trying to sort vector<time_t> assending.
the following is by best go but needs some guide.

struct assending : public binary_function<time_t, time_t, bool> {
bool operator()(time_t x, time_t y) {
return x > y;
}
}


sort(time_vec.begin(), time_vec.end(), assending());

will this be the correct way to do it?

thanks

There is a standard version of your "assending" functor, called
greater. All you need to do is:

#include <algorithm>
// ...
sort(time_vec.begin(), time_vec.end(), greater<time_t>()) ;

See this link for details:
http://www.sgi.com/tech/stl/greater.html
 
P

peter koch

dear programmers
I am in need of some help please.
I am trying to sort vector<time_t> assending.
the following is by best go but needs some guide.

struct assending : public binary_function<time_t, time_t, bool> {
bool operator()(time_t x, time_t y) {
return x > y;
}

As mentioned otherwere, this functionality is already present in the
std:: library.
}

sort(time_vec.begin(), time_vec.end(), assending());

will this be the correct way to do it?

No - this sorts in descending order. the correct way is
std::sort(time_vec.begin(), time_vec.end());
Plain and as simple as it can be right now.

/Peter
 

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

Similar Threads


Members online

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,294
Latest member
LandonPigo

Latest Threads

Top