![]() |
| | #1 |
| Registered User Join Date: Jul 2009
Posts: 24
| using set of class obj to sort by obj member I have a Class which one of its attributes is a date - Class reservations members - name - date lets say I have multiple restaurants and want to insert class reservations into the set - but have it automatically sort by date. can I insert pointers to the class object into the set and have it automatically sort by the date attribute? many thanks |
| lawrenced is offline | |
| | #2 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| You are using this magic word "set". Do you have a set, somewhere? If so, then yes it is sorted, although you will have to define the ordering yourself. (If you really mean that you have a set of pointers, then you will have to define a function object; if you are inserting the class it would be possible for you to define operator < for the class.) |
| tabstop is offline | |
| | #3 |
| Registered User Join Date: Jul 2009
Posts: 24
| I am new to c++ so I don't fully understand - I will be creating set<Reservations*> restaurant1; I want to randomly allocate vector<Reservations*> reservationsmade; to set<Reservations*> restaurant1; or set<Reservations*> restaurant2; but want it to automatically sort by reservationsmade[rsvpnumber]->Getdate(); when i insert it into the set. so that when I later get for(int table = 0; table < totalrsvpsatrestaurant; ++table) { restaurant1[table]->Getdate() } it will display dates in sequential order... |
| lawrenced is offline | |
| | #4 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| You'll have to decide whether you want a set or a vector. Sets automatically sort, but you don't get to use [brackets]. Vectors allow you to use [brackets], but you will have to keep them sorted yourself. Either way, you will need to write a function that takes two pointers-to-Reservations and "compares" them -- decides which one comes first. (I am assuming that you have made a conscious design decision to use pointers-to-Reservations; at the moment it seems supremely silly but I'm trusting you that there's a reason.) |
| tabstop is offline | |
| | #5 | |
| Registered User Join Date: Jul 2009
Posts: 24
| Quote:
c++ does not have an STL that will find the best place to insert the object in a list based on some sortable criteria? i was originally going to use list, and run through the list comparing dates, but speed is very critical. set will not automatically sort once i insert? | |
| lawrenced is offline | |
| | #6 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
(However, if you are storing pointers, pointers already come with a "<" defined -- whichever one comes first in memory. You will have to specify a function object if you wish to store pointers in your set.) | |
| tabstop is offline | |
| | #7 |
| Registered User Join Date: Jul 2009
Posts: 24
| i am confused on syntax. may you offer some example code as help please? |
| lawrenced is offline | |
| | #8 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Well. The most obvious thing would be this: Code: class Reservation {
//whatever
};
bool operator< (Reservation left, Reservation right) {
/* I'm assuming that getDate gets some sort of timestamp */
/* If not, you can put your date logic here */
return left.getDate() < right.getDate();
}
std::set<Reservation> reservationsmade;
|
| tabstop is offline | |
| | #11 |
| Guest Join Date: Aug 2001
Posts: 4,923
| >> That's the most ... instructive ... example I've ever seen. What? It looks ok to me. |
| Sebastiani is offline | |
| | #12 | ||
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Quote:
And lawrenced: avoid pointers in the first place. Just create your Reservations and put them into the set. No new. No delete.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| ||
| Elysia is offline | |
| | #13 |
| Registered User Join Date: Jul 2009
Posts: 24
| Elysia - thank you very much for suggestion - Reservation will grown to have many characteristics - very large class with many private data members - i think i will want to keep it as pointer for efficiency? |
| lawrenced is offline | |
| | #14 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Pointers are only necessary if you're going to move around data a lot, for example assigning a set to another. However, as the rule goes, start without pointers first, and if you application is running slow (you can use a profiler), then optimize it.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
![]() |
| Tags |
| date, set, sort |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| deriving classes | l2u | C++ Programming | 12 | 01-15-2007 05:01 PM |
| C help for network animator | fastshadow | Tech Board | 7 | 03-17-2006 03:44 AM |
| Polymorphism Theory Question - Polymorphic Class Definition. | ventolin | C++ Programming | 3 | 10-31-2005 12:05 PM |
| Problem with Visual C++ Object-Oriented Programming Book. | GameGenie | C++ Programming | 9 | 08-29-2005 11:21 PM |
| how should I set the constructor in order to inherit the base class? | gogo | C++ Programming | 1 | 11-27-2001 01:07 PM |