vector and binary predicates

This is a discussion on vector and binary predicates within the C++ Programming forums, part of the General Programming Boards category; Or rather not using binary predicates... I'm probably missing something really simple here, but I can't see what I am ...

  1. #1
    Registered User dalek's Avatar
    Join Date
    May 2003
    Posts
    135

    vector and binary predicates

    Or rather not using binary predicates...

    I'm probably missing something really simple here, but I can't see what I am doing wrong.

    When using the STL vector template you can use the algorithm sort (defined in <algorithm>) to sort the vector. There are two versions available, one that takes a binary predicate as an argument and one that does not.

    Now if you don't supply the binary predicate, the algorithm will attempt to sort using the < operator if it is defined which works fine if you have a vector of objects. BUT - if I have a vector of pointers to objects, it wont sort, in fact the operator< method is never called.
    Code:
    	bool operator<(const CFoo &f)
    	{
    		if (data < f.data)
    			return true;
    		else
    			return false;
    	}
    Now the above code works a treat for a vector declared as vector<CFoo> vec;

    But if I have a vector declared as vector<CFoo *> vec; I can't seem to rewrite the operator. For a binary predicate it is simply a case of changing the predicate to a dereferencing predicate, but it doesn't appear to be case here as below...
    Code:
    bool operator<(const CFoo *f)
    {
      if (data < f->data)
        return true;
       else 
         return false;
    }
    Any really obvious mistakes pointed out are appreciated!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Code:
    bool MySort(const CFoo* foo1, const CFoo* foo2)
    {
        return foo1->data < foo2->data;
    }
    ...
    vector<CFoo*> vec;
    ...
    sort(vec.begin(),vec.end(),MySort);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User dalek's Avatar
    Join Date
    May 2003
    Posts
    135
    Sorry, might not have made myself clear ( I do ramble) - thanks for your response though..

    I know how to do this with a dereferencing binary predicate, I was wondering though if it is possible to have the object be responsible for determining whether it should be sorted (i.e. using the operator< method).

    Using the operator< method works fine if you are using references, but the second you use pointers a dereferencing operator< method does not seem to work, probably due to an error of mine, but I can't see the error.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Code:
    class CFoo
    {
        int data;
    public:
        bool operator<(const CFoo *f)
        {
            if (data < f->data)
                return true;
            else
                return false;
        }
    };
    ...
    vector<CFoo*> vec;
    ...
    sort(vec.begin(),vec.end());
    As defined, you cannot have the above less-than operator used in the sort function, I'm not sure it would even ever make any sense to have something like that. The container stores pointers to objects and the comparison needs to be geared towards that... comparing pointers-to-objects with other pointers-to-objects. What you seem to be trying to do with the above operator is comparing objects with pointers-to-objects which won't work.

    [edit]I guess the above operator could be used in the following manner but it seems odd to have a real need for it:
    Code:
    CFoo  foo1;
    CFoo  foo2;
    CFoo* foo2ptr = &foo2;
    ...
    if( foo1 < foo2ptr )  // Seems an odd/uncommon usage here
    {
        // Do stuff
    }
    [/edit]
    Last edited by hk_mp5kpdw; 08-17-2004 at 07:51 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

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