Trouble sorting a list

This is a discussion on Trouble sorting a list within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <algorithm> #include <fstream> #include <iterator> #include <string> #include <vector> #include <list> bool strLesser(const std::string &elem1, const ...

  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 03:11 PM.

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

    I dont think std::vector has sort() and unique() member functions.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 11: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, 11:21 PM

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