-
December 12th, 2012, 01:09 PM
#1
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..
-
December 12th, 2012, 07:32 PM
#2
Re: Using Comparator in Set
Originally Posted by Rajesh1978
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 << ∁ // 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 08:01 PM.
-
December 13th, 2012, 12:40 AM
#3
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.
-
December 13th, 2012, 01:36 AM
#4
Re: Using Comparator in Set
The parameters should be const reference, not non-const reference.
-
December 13th, 2012, 01:48 AM
#5
Re: Using Comparator in Set
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|