sorting a vector

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BEEMZA
    New Member
    • Apr 2007
    • 8

    sorting a vector

    Hi All
    I was just wondering if its possible to use the sort method found in the STL algorithms class on a vector I declared as: " vector<myClass> myVector; "
    where I want to sort according to one of the instance varaibles of myClass.

    Thanks
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by BEEMZA
    Hi All
    I was just wondering if its possible to use the sort method found in the STL algorithms class on a vector I declared as: " vector<myClass> myVector; "
    where I want to sort according to one of the instance varaibles of myClass.

    Thanks
    Sure, simply define an overloaded operator< for your class and sort the vector
    given the two iterators v.begin() and v.end().

    kind regards,

    Jos

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      If you don't have operator< implemented in your class, then you can use the sort algorithm where you pass in iterators to the begin and end of the sort range and a binary predicate.

      The binary predicate is a function you write that takes two or your vector elements as arguments and returns true if the first argumenrt is less than the second argument. The sort algorithm will call this function to compare your vector elements.

      Comment

      • BEEMZA
        New Member
        • Apr 2007
        • 8

        #4
        sorting vector

        Hi All

        I posted earlier.....
        I took advise on using the sort method in the STL under algorithms on a class that I created. Here is the operator overloading that i did but i cant seem to get it to sort at all. PLease help

        Code:
        bool info::operator < (info &b){
        	if ( (*this).getWeight() < b.getWeight() )
        	return true;
        	else
        	return false;
        }
        Last edited by AdrianH; Apr 22 '07, 02:25 AM. Reason: Added code tags

        Comment

        • AdrianH
          Recognized Expert Top Contributor
          • Feb 2007
          • 1251

          #5
          Originally posted by BEEMZA
          Hi All

          I posted earlier.....
          I took advise on using the sort method in the STL under algorithms on a class that I created. Here is the operator overloading that i did but i cant seem to get it to sort at all. PLease help

          Code:
          bool info::operator < (info &b){
          	if ( (*this).getWeight() < b.getWeight() )
          	return true;
          	else
          	return false;
          }
          I don't know the original problem. I'll have to look back to find it but am about to go to bed, so I'll do it tomorrow.

          What you have posted probably should work. Did you get any compile errors, or warnings? If you are using gnu c++, try using the -Wall switch. It will spit out anything that looks suspicious.

          One thing I would change which may be a factor is that the parameter should be constant as should the function you overloaded.

          Other than that, the (*this).getWeig ht() can be expressed as simply getWeight() and you could just use return getWeight() < b.getWeight() instead of breaking it up with an if statement. However, your method can be useful when using certain debuggers.

          Let me know if this helps. Otherwise, I'll see if I can read up on your last thread.


          Adrian

          Comment

          • AdrianH
            Recognized Expert Top Contributor
            • Feb 2007
            • 1251

            #6
            Hi BEEMZA,

            I would like to say to you and all other posters that we at The Scripts Developer Network (TSDN) are happy to help you with your problems.

            However, it is inappropriate to PM (send a personal message) to anyone regarding a question that should be posted to a forum unless explicitly requested to do so. There are many reasons for this including, a) the person that you are PMing may not be around all the time, b) if everyone PMed instead of using the forums then others may not be able to help or learn from your problems. Also, it may flood a single users PM box which is not good either.

            Please look at the posting guidelines to learn the ways of the site and reduce the risk of offending other users, no matter what their relationship to the site is.
            Unfortunately, I feel that I cannot continue this thread until you pass the information you passed on to me to this forum, as an act that you understand what I am saying.

            I hold no hard feelings towards you, as I hope you do not to me either.


            Adrian

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              I would like to see your sort() call.

              Comment

              • BEEMZA
                New Member
                • Apr 2007
                • 8

                #8
                sorting vector continued.....

                I am trying to sort a vector containing objects of a class, using one of the instance variables as a key(int).

                I defined the operator overload as follows:

                Code:
                bool info::operator < (info &b){
                	return getWeight() < b.getWeight();
                	
                }
                In the driver i have included <algorithm> and am using the sort method as follows:

                sort ( myVector.begin( ), myVector.end() )

                but this does not sort the vector at all.

                Pls help
                Last edited by AdrianH; Apr 22 '07, 08:55 PM. Reason: Added code tags

                Comment

                • AdrianH
                  Recognized Expert Top Contributor
                  • Feb 2007
                  • 1251

                  #9
                  Hi again,

                  When replying to a message, please do not start a new thread. Click on the reply button at the bottom of the thread listing. What you are doing is considered double posting and is frowned upon (you can get booted off the site if it continues). At this point, I am assuming that you are having difficulties understanding how to post as this is the second time I have merged this thread.

                  Please reply to this thread so that we can help you. The post will come up to the beginning of the list of posts when someone (you or anybody else) replies to the thread, so you do not have to start a new thread to get our attention. Right now you are getting the wrong attention, the type that doesn't make me want to help you. So please smarten up, as my patience has its limits.

                  As for sorting, see http://www.cplusplus.com/reference/algorithm/sort.html for more information.

                  Thank you,


                  Adrian

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    This looks correct:
                    Code:
                    sort ( myVector.begin(), myVector.end() )
                    I would now like to see your class declaration and your main().

                    I assume you have put a breakpoint in your info::operator< and run the the programusing your debugger to verify that the info::operator< function is being called by the sort. Yes?

                    Comment

                    Working...