Thread: sort

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    56

    sort

    How would I sort a vector of strings by the size of the string? pal is a vector containing strings. I call sort using the function greater to sort the strings by size.
    Code:
    psuedocode:
    
    sort(pal.begin(), pal.end(), greater);
    
    .................................
    
    bool greater(string a, string b)
    {
    	int i, j;
    	i = a.size();
    	j = b.size();
    	return i > j;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How does it not work?

    By the way, you should be passing the strings by const reference in the comparator.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The idea is correct.

    You could rename the comparison function to something like longer to describe better what it does (there is already std::greater and it means comparing with > operator) and shorten it just to a single return statement.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Just like that?

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    56
    I was getting an error, reference to ‘greater’ is ambiguous, along with several other errors. When I changed greater to longer it works fine. I didn't realize there was already a std::greater. Thanks.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by swappo
    I was getting an error, reference to ‘greater’ is ambiguous, along with several other errors.
    Ah well, at least now you know that you should post about the problem, including any error messages

    Incidentally, you should consider turning the comparator into a function object:
    Code:
    struct Longer
    {
        bool operator()(const std::string& lhs, const std::string& rhs) const
        {
            return lhs.size() > rhs.size();
        }
    };
    You could easily turn it into a class template too, if you see a need to do so.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by swappo View Post
    I was getting an error, reference to ‘greater’ is ambiguous, along with several other errors. When I changed greater to longer it works fine. I didn't realize there was already a std::greater. Thanks.
    Are you including the <functional> header and using a using namespace std; directive?
    If so, it probably sees your greater() function as well as std::greater and doesn't know which one you're talking about.

    Try renaming your function to longer() as previously suggested, or get rid of using namespace std;
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cpjust
    Try renaming your function to longer() as previously suggested, or get rid of using namespace std;
    Or fully qualify the global function as ::greater, but frankly renaming it is the best option even if there was no name collision.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  2. threaded merge sort
    By AusTex in forum Linux Programming
    Replies: 4
    Last Post: 05-04-2005, 04:03 AM
  3. Sorting
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-10-2003, 05:21 PM
  4. radix sort and radix exchange sort.
    By whatman in forum C Programming
    Replies: 1
    Last Post: 07-31-2003, 12:24 PM
  5. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 08:05 PM