C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-22-2009, 10:15 PM   #1
Registered User
 
Join Date: Jul 2009
Posts: 24
using set of class obj to sort by obj member

hi,

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   Reply With Quote
Old 07-22-2009, 10:41 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
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   Reply With Quote
Old 07-22-2009, 10:50 PM   #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   Reply With Quote
Old 07-22-2009, 11:01 PM   #4
and the Hat of Guessing
 
tabstop's Avatar
 
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   Reply With Quote
Old 07-22-2009, 11:14 PM   #5
Registered User
 
Join Date: Jul 2009
Posts: 24
Quote:
Originally Posted by tabstop View Post
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.)

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   Reply With Quote
Old 07-22-2009, 11:20 PM   #6
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Quote:
Originally Posted by lawrenced View Post
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?
When I said "sets automatically sort" I meant exactly what I said. If you were storing, say, ints or floats it would be completely automatic. But since you just made up a Reservation object out of your own head, you also have to specify how they get sorted. (After all, presumably there's more than one member of this Reservation object. How is the machine to know it isn't supposed to sort by number in party? Or last name? Or the order they were created?) That's why you are allowed to define "<" for new objects like that.

(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   Reply With Quote
Old 07-22-2009, 11:27 PM   #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   Reply With Quote
Old 07-22-2009, 11:32 PM   #8
and the Hat of Guessing
 
tabstop's Avatar
 
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   Reply With Quote
Old 07-22-2009, 11:33 PM   #9
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 4,923
Example.
Sebastiani is offline   Reply With Quote
Old 07-22-2009, 11:37 PM   #10
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Quote:
Originally Posted by Sebastiani View Post
That's the most ... instructive ... example I've ever seen.
tabstop is offline   Reply With Quote
Old 07-23-2009, 01:43 AM   #11
Guest
 
Sebastiani's Avatar
 
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   Reply With Quote
Old 07-23-2009, 03:01 AM   #12
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Quote:
Originally Posted by tabstop View Post
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;
operator < should accept two const references for efficiency's sake.
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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 07-23-2009, 08:47 PM   #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   Reply With Quote
Old 07-24-2009, 03:48 AM   #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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Tags
date, set, sort

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:17 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22