Thread: Sorting MyClass

  1. #16
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187
    These Changes shouldn't have any effect buy they do !

    and now it's not working properly

    in main()
    Sstring a("ANumber One"); ---> Sstring a("cccc");
    Sstring b("BNumber Two"); ---> Sstring a("aaa");
    Sstring c("CNumber Three"); ---> Sstring a("bbb");

    in add funct
    theList.push_front(IN); ---> theList.push_back(IN);

  2. #17
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    are you sure you want three Sstring variables all called a, rather than one called a, one called b, and one called c????

  3. #18
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187
    copy and paste error..thanks


    These Changes shouldn't have any effect buy they do !

    and now it's not working properly

    in main()
    Sstring a("ANumber One"); ---> Sstring a("cccc");
    Sstring b("BNumber Two"); ---> Sstring b("aaa");
    Sstring c("CNumber Three"); ---> Sstring c("bbb");

    in add funct
    theList.push_front(IN); ---> theList.push_back(IN);

  4. #19
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >These Changes shouldn't have any effect buy they do

    No, they'd don't. it appears my workaround isn't working. The derived greater<Sstring*> is never being called. It was luck that they last one worked. All I can suggest at the moment is create a list of Sstring's rather than Sstring*'s, or use a more standards compliant compiler.

  5. #20
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Sorenson: in your post

    Code:
    class s
    {
    public:
    	bool operator()(const Sstring* a, const Sstring* b){ 
    			return *a < *b; 
    		} 
    };
    
    class Holder{
    	private:
    		list<Sstring *> theList;
    	
    	public:
    		void addMe(Sstring *IN){
    			theList.push_front(IN);
    		}
    
    		void dsp_all(){
    			list<Sstring *>::iterator i = theList.begin();
    			cout<<endl<<"--TOP--"<<endl<<endl;
    			while(i != theList.end()){
    				(*i)->dsp();//toStr;
    				cout<<endl;
    				i++;
    			}
    			cout<<endl<<"--END--"<<endl;
    		}
    
    		void ListSort(){
    			cout<<"Holder::ListSort()"<<endl;
    
    			theList.sort(s());
    		}
    what if you declared an instance of type s to send to list::sort() rather than trying to send the class name itself? Something like this:


    Code:
    void ListSort(){
    			cout<<"Holder::ListSort()"<<endl;
    
                                                    s functionObject;
    			theList.sort(functionObject());
    		}
    Sorry, don't have compiler capable of using STL so can't compile the code myself.

  6. #21
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187
    You'll get :

    Error E2193 srt2.cpp 63: Too few parameters in call to 's:perator ()(const Sstring *,const Sstring *)'
    in function Holder::ListSort()


    Other suggestions are extreamly welcomed !!!

  7. #22
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    Simply specialize greater for Sstring*:
    Code:
    #include <iostream>
    #include <algorithm>
    #include <functional>
    #include <list>
    #include <cstring>
    
    using namespace std;
    
    class Sstring {
        char* toStr;
    
    public:
        Sstring(const char* s) {
            toStr = new char[strlen(s) + 1];
            strcpy(toStr, s);
        }
        
        ~Sstring() {
            delete[] toStr;
        }
        
        void dsp() const { cout << toStr; }
        
        bool operator<(const Sstring& s) const {
            // Are you really sure you want to sort based on first letter only?
            return toStr[0] < s.toStr[0];
        }   
    };
    
    template<>
    struct std::greater<Sstring*> // don't ask me why it needs the std:: part
    : public binary_function<const Sstring*, const Sstring*, bool> {
        bool operator()(const Sstring* a, const Sstring* b) const {
            return *a < *b;
        }
    };
    
    class Holder{
        list<Sstring*> theList;
        
    public:
        void addMe(Sstring* IN) {
            theList.push_back(IN);
        }
        
        void dsp_all() const {
            list<Sstring*>::const_iterator i = theList.begin();
            cout << endl << "--TOP--" << endl << endl;
            while(i != theList.end()) {
                (*i)->dsp();
                cout << endl;
                i++;
            }
            cout << endl << "--END--" << endl;
        }
        
        void ListSort() {
            cout << "should be sorted";
            theList.sort(greater<Sstring*>());
        }
    };
    
    int main() {
        Holder h;
        
        Sstring a("cccc");
        Sstring b("aaaa");
        Sstring c("bbbb");
    
        h.addMe(&a);
        h.addMe(&b);
        h.addMe(&c);
    
        h.dsp_all();
    
        h.ListSort();   
    
        h.dsp_all();
    
        return 0;
    }
    Other than that, find a conforming compiler: CodeWarrior, Comeau, g++ are all excellent choices.
    - lmov

  8. #23
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187
    Excellent..... I knew there had to be a way.

    Can someone explain the
    template<>struct std::greater<Sstring*>

    for those of us who aren't too sure how it works.

  9. #24
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >Can someone explain the
    template<>struct std::greater<Sstring*>

    It's a specialisation of the pre-defined greater functor provided by the standard library. When defining templates you can specify special cases that will get called if they are instantiated by certain types. As the MSVC 6.0 compiler seemingly refuses to allow user defined functors (or any other standard library functor apart from greater) or function pointers to be used as an argument to std::list::sort(), it is a workaround.

    This has little bearing on how your code should work, and wouldn't be necessary on most other C++ compilers (including MSVC 6.0's successor).

  10. #25
    Unregistered
    Guest
    #include <algorithm>
    #include <functional>

    aren't needed for the above code

  11. #26
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    #include <algorithm>
    #include <functional>

    aren't needed for the above code
    algorithm isn't needed, but functional is for greater and binary_function. As list includes functional you don't have to explicitly include it, but doing so does no harm and it could be argued that it would be beneficial to explicitly include it so that someone reading the code knows you're using something declared in it directly rather than indirectly through a different class/template.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Sorting Algorithm Help
    By cjwenigma in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2007, 02:04 PM
  3. vector: sort multiple fields
    By FoodDude in forum C++ Programming
    Replies: 4
    Last Post: 09-28-2005, 11:00 AM
  4. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  5. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM