Thread: Comparing Objects:

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499

    Comparing Objects:

    I have a class that has a few variables and getters / setters. I set these variables and then place the node into a vector.

    Code:
            vehicle.setLicensePlate(plate);
            vehicle.setCamera(camera);
            vehicle.setTime(time);
            
            VectorVehicle.push_back(vehicle);
    Here is the issue, I am using std::sort and want to sort by the licensePlates like this:

    Code:
    class Calculate {
    private:
        std::string line;
        std::vector<std::string> readerVector;
        Vehicle vehicle;
        
        struct{
        
            inline bool operator() (std::vector<Vehicle> v1, std::vector<Vehicle> v2)
            {
                return v1.getLicensePlate == v2.getLicensePlate;
            };
        
        }licensePlateObject;
        
    public:
        Calculate();
        void readFile();
        void calculateSpeed();
        int verifyOutFile(std::istream &reader);
        void sortVector(std::vector<Vehicle> v);
        
    };
    In Calculate Class:

    Then sort this vector by license plates
    Code:
    void Calculate::sortVector(std::vector<Vehicle> v){
        
        std::sort(v->getLicensePlate.begin(), v.getLicensePlate.end(),licensePlateObject);
        
    }

    I know my sort method is incorrect, I have tried a few different ways but it is not working. Just looking for some advice.

  2. #2
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    I got it..... I really want to take this post down but for the sake of others learning I will post the answer:

    That is how you compare objects in a class using the sort function.
    Code:
     struct{
        
            inline bool operator() (Vehicle v1, Vehicle v2)
            {
                return v1.getLicensePlate() == v2.getLicensePlate();
            };
        
        }licensePlateObject;
    Code:
    void Calculate::sortVector(std::vector<Vehicle> v){
        
        std::sort(v.begin(), v.end(),licensePlateObject);
        
    }

  3. #3
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Since no one has responded yet I still need a little advice. The above is sorting the times, not the string of license plates. Another thing that makes me believe I mishandled this is that it take about 4 whole seconds to sort 50,000 plates. I wrote this same program in Java and it took only 1/2 sec.

    I know that std::sort uses quick sort and I do not think this is the issue, but what I am really confused about is that is only sorts the time not the plates like it is suppose too.

    I even tried:
    Code:
     struct{
        
            inline bool operator() (Vehicle v1, Vehicle v2)
            {
                return v1.getLicensePlate().compare(v2.getLicensePlate());
            };
        
        }licensePlateObject;
    and I got the same result it is not sorting anything...
    Last edited by jocdrew21; 11-14-2014 at 02:54 PM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,364
    A couple things...

    1) Your sorting operator should return true if v1 is less than v2 and false otherwise. Using == or compare does not do this.
    2) You should be passing by reference or reference to const. For example, Calculate::sortVector(std::vector<Vehicle> v) sorts the vector v, but that doesn't do anything to whatever you passed to the function because v is a copy. This isn't like Java, objects are passed by value by default.
    3) If your vector is being sorted by time, it's probably because your sort function isn't working at all due to #1 and #2, but you may want to make sure getLicensePlate is returning the license plate and not the time.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Sorting can't be done with a test for equality.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Thank you for the replies and the reminder of pass by value vs. reference, I have since fixed the issue.

    Code:
    struct{
        
            inline bool operator() (Vehicle v1, Vehicle v2)
            {
                return v1.getLicensePlate().compare(v2.getLicensePlate());
            };
        
        }licensePlateObject;
    I used this above struct to comparing string but you guys are telling me this won't work. When I run it, it does sort some of the strings, but it is very inconsistent and runs very slow, about 4-5 seconds to compile. How would you sort 70,000 nodes in a vector but the string value in each of them?

    The end statement is I want all the like Vehicle nodes to be together so I can calculate their speed by the time they pass each camera.

  7. #7
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Code:
    struct{
        
            inline bool operator() (Vehicle v1, Vehicle v2)
            {
                //return v1.getLicensePlate().compare(v2.getLicensePlate());
                return v1.getLicensePlate() < v2.getLicensePlate();
            };
        
        }licensePlateObject;
    This works.... Sort of, thanks for the help everyone.

  8. #8
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    I am still working two issues, one is it is sorting the licensePlates together now but not in the order I would like here is an example of what the output is:
    Code:
    315630 4 21 17 34.\
    315630 2 21 16 55.\
    315630 3 21 17 08.\
    315630 1 21 16 27.\
    315630 5 21 17 59.\
    I am trying to figure out a way to sort them in order by licensePlates and camera order 1-5 so I can calculate their speed.

    Second issue is that I am able to get rid of the ':' char in the time but now I am trying to convert it to a int. I have used a few ways and after reading several forums and googling my way through it, it comes back to std::stoi. However it is simply not working. Am I using it wrong? I thought it could be the way I was combining char but string are mutable and I never had a issue combining string like this in the past.

    Code:
    int Vehicle::getTime(){
        std::string s_hour,s_min,s_sec;
        int hour,min,sec,time;
        
        //cleaning time format from 05:33:26 to 05 33 26
    
        for (int i=0; i<sizeof(vtime); i++) {
            if (vtime[i]==':') {
                vtime[i]=' ';
            }
        }
        
        //set time to hour, minutes and seconds skiping vtime[2] and [5]
        //because they are now spaces
        s_hour = vtime[0]+vtime[1];
        s_min= vtime[3]+vtime[4];
        s_sec=vtime[6]+vtime[7];
        
        //convert string to int
        hour=std::stoi(s_hour);
        min=std::stoi(s_min);
        sec=std::stoi(s_sec);
        
        
        //convert time to total seconds
        time= (hour * 60 * 60) + (min * 60) + sec;
        
        return time;
    }
    If I convert a char to a int, it works. Why is stoi not working?
    Code:
    int Vehicle::getTime(){
        char s_hour,s_min,s_sec;
        int hour,min,sec,time;
        
        //cleaning time format from 05:33:26 to 05 33 26
    
        for (int i=0; i<sizeof(vtime); i++) {
            if (vtime[i]==':') {
                vtime[i]=' ';
            }
        }
        
        //set time to hour, minutes and seconds skiping vtime[2] and [5]
        //because they are now spaces
        s_hour = (int)vtime[0]+(int)vtime[1];
        s_min= (int)vtime[3]+(int)vtime[4];
        s_sec=(int)vtime[6]+(int)vtime[7];
        
        //convert string to int
        hour=s_hour;
        min=s_min;
        sec=s_sec;
        
        //convert time to total seconds
        time= (hour * 60 * 60) + (min * 60) + sec;
        
        return time;
    }
    Last edited by jocdrew21; 11-15-2014 at 05:23 AM.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,498
    Second issue is that I am able to get rid of the ':' char in the time but now I am trying to convert it to a int. I have used a few ways and after reading several forums and googling my way through it, it comes back to std::stoi. However it is simply not working. Am I using it wrong? I thought it could be the way I was combining char but string are mutable and I never had a issue combining string like this in the past.
    The stox() functions require a C++11 standard compiler, are you sure you're compiling with a compliant compiler? Why are you storing these entries into a string instead of storing them into integers when you process the file?

    Code:
        for (int i=0; i<sizeof(vtime); i++) {
            if (vtime[i]==':') {
                vtime[i]=' ';
            }
        }
    What is a vtime? Is it a std::string, a C-string, what? If it's a std::string then the sizeof() operator is probably not doing what you think. And if it isn't a std::string why?

    If vtime is a std::string I recommend you use a stringstream to process the string to convert the individual elements to the int. I've already shown you a way of doing this in one of your other topics.

    And again like most of your posts you have failed to provide all the relevant code which makes this all a guessing game.

    I am trying to figure out a way to sort them in order by licensePlates and camera order 1-5 so I can calculate their speed.
    What have you tried?

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encapsulation and objects within objects
    By nair in forum C++ Programming
    Replies: 4
    Last Post: 07-31-2011, 10:42 AM
  2. Replies: 1
    Last Post: 05-24-2011, 06:36 PM
  3. comparing object to a growing set of other objects
    By mc61 in forum C++ Programming
    Replies: 5
    Last Post: 05-24-2008, 04:35 PM
  4. Assigning objects to objects
    By Swordsalot in forum C++ Programming
    Replies: 4
    Last Post: 07-26-2006, 03:47 AM
  5. Replies: 4
    Last Post: 10-16-2003, 11:26 AM