using set of class obj to sort by obj member

This is a discussion on using set of class obj to sort by obj member within the C++ Programming forums, part of the General Programming Boards category; hi, I have a Class which one of its attributes is a date - Class reservations members - name - ...

  1. #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

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.)

  3. #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...

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.)

  5. #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?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.)

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    24
    i am confused on syntax. may you offer some example code as help please?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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;

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,707
    Code:
    #include <cmath>
    #include <complex>
    bool flip(bool value)
    {
           return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0)*(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Sebastiani View Post
    That's the most ... instructive ... example I've ever seen.

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,707
    >> That's the most ... instructive ... example I've ever seen.

    What? It looks ok to me.
    Code:
    #include <cmath>
    #include <complex>
    bool flip(bool value)
    {
           return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0)*(1 << (value + 2)))
        ).real() < 0;
    }

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #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?

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  2. C help for network animator
    By fastshadow in forum Tech Board
    Replies: 7
    Last Post: 03-17-2006, 02:44 AM
  3. Replies: 3
    Last Post: 10-31-2005, 11:05 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Replies: 1
    Last Post: 11-27-2001, 12:07 PM

Tags for this Thread


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