Using Comparator in Set
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Using Comparator in Set

  1. #1
    Join Date
    Jul 2007
    Posts
    225

    Lightbulb Using Comparator in Set

    I have the piece of code
    Code:
    #include<iostream>
    #include<set>
    
    using namespace std;
    
    class Test
    {
            public:
                    int val;
    };
    
    class comp
    {
            bool operator()(Test a, Test b)
            {
                    return a.val < b.val;
            }
    };
    
    bool cmp(Test a, Test b)
    {
            return a.val<b.val;
    }
    int main()
    {
            set<Test,comp>s;
            return 0;
    }
    
    It comiles fine
    
    but 
    
    int main()
    {
            set<Test,cmp>s;
            return 0;
    }
    gives following error
    
    setTest.cpp: In function ‘int main()’:
    setTest.cpp:26: error: type/value mismatch at argument 2 in template parameter list for ‘template<class _Key, class _Compare, class _Alloc> class std::set’
    setTest.cpp:26: error:   expected a type, got ‘cmp’
    setTest.cpp:26: error: invalid type in declaration before ‘;’ token
    My doubt is why we can only use functor and not predicate function.

    because in algorithms we can use both functor and predicate.

    help me understanding it..

  2. #2
    Join Date
    Apr 1999
    Posts
    27,424

    Re: Using Comparator in Set

    Quote Originally Posted by Rajesh1978 View Post
    My doubt is why we can only use functor and not predicate function.

    because in algorithms we can use both functor and predicate.

    help me understanding it..
    "comp" is a type. "cmp" is a function, and functions are values. That's the difference.
    That second template argument for set is asking for a type, not a value.

    Here is a simple example of this:
    Code:
    #include <iostream>
    #include<set>
    
    using namespace std;
    
    class Test
    {
            public:
                    int val;
    };
    
    class comp
    {
            bool operator()(Test a, Test b)
            {
                    return a.val < b.val;
            }
    };
    
    bool cmp(Test a, Test b)
    {
            return a.val<b.val;
    }
    
    int main()
    { 
       cout << &cmp;    // compiles
       cout << &comp;  // does not compile
    }
    I can take the address of cmp, but not comp since comp is not a value.
    Code:
    int main()
    { 
       comp MyComp;
       set<Test, MyComp> s; // now this doesn't compile
    }
    Now the code that uses comp doesn't compile for the same reason. MyComp is now a value (object instance) and not a type, therefore it can't be used as a second parameter to the set functor. The last example is essentially what you're trying to do with the simple function example.

    To solve your problem:
    Code:
    typedef bool (*fn)(Test, Test);
    
    int main()
    {
       set<Test, fn> s(cmp); 
       return 0;
    }
    I defined a function type that takes two Test parameters and returns a bool. Then in the template's second parameter I give the type, and the set constructor I give the actual function to use.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; December 12th, 2012 at 07:01 PM.

  3. #3
    Join Date
    Jul 2007
    Posts
    225

    Re: Using Comparator in Set

    Thanks.
    I got it now.

    while testing this i got one more confusion.

    if i make the comp as below

    Code:
    class comp
    {
            bool operator()(Test &a, Test &b) // Added reference to Test 
            {
                    return a.val < b.val;
            }
    };
    and try to compile it is giving me a lot of errors

    My full code

    Code:
    #include<iostream>
    #include<set>
    
    using namespace std;
    
    class Test
    {
        public:
            Test(int v):val(v){}
            int val;
    };
    
    class comp
    {
        public:
        bool operator()(Test &a, Test &b)
        {
            return a.val < b.val;
        }
    };
    
    bool cmp(Test a, Test b)
    {
        return a.val<b.val;
    }
    int main()
    {
        set<Test,comp>s;
        Test t1(10),t2(11),t3(9),t4(-11);
        s.insert(t1);
        s.insert(t2);
        s.insert(t3);
        s.insert(t4);
        return 0;
    }

    what difference it makes if i use the reference in the bracket operator overloading.
    as I am passing an object I wanted to use the reference.
    Is reference not permitted while overloading the '()' operator.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,261

    Re: Using Comparator in Set

    The parameters should be const reference, not non-const reference.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jul 2007
    Posts
    225

    Re: Using Comparator in Set

    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center