Thread: Trouble sorting a list

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    21

    Trouble sorting a list

    Code:
    #include <iostream>
    #include <algorithm>
    #include <fstream>
    #include <iterator>
    #include <string>
    #include <vector>
    #include <list>
    
    bool strLesser(const std::string &elem1, const std::string &elem2) const
    {
         return elem2.size() > elem1.size();
    }
    
    int main()
    {
    std::list<std::string> WordList;
    
    WordList.push_back("Hello");
    WordList.push_back("This");
    WordList.push_back("is");
    WordList.push_back("Just");
    WordList.push_back("Hello");
    WordList.push_back("a");
    WordList.push_back("Test");
    WordList.push_back("a");
    
    // Alphabetical sort first
    WordList.sort();
    // Remove duplicates
    WordList.unique();
    // Character count sort
    std::stable_sort(WordList.begin(), WordList.end(), strLesser);
    
    return 0;
    }
    When I ran the above code I got this error message 3 times.
    Code:
    C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\algorithm(1591): error C2064: term does not evaluate to a function taking 2 arguments
    I even tried using sort instead of stable_sort but I got even more error messages. I can't figure out where that error is coming from; when I clicked on it it took me to the sort generic algorithm so that didn't help there. Thanks for the help.

    My mistake, when I posted this earlier I mistyped the the wordlist as vector. Anyways, I get the errors above with a list still.
    Last edited by pliang; 07-17-2005 at 02:11 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hmm... strLesser() shouldnt be const since it isnt a member function.

    I dont think std::vector has sort() and unique() member functions.
    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. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  2. linked list sorting
    By ronenk in forum C Programming
    Replies: 7
    Last Post: 12-14-2004, 09:49 AM
  3. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM
  4. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM