Re: Asymmetric Functor predicates
And VC10 conforms to C++0x here. Upgrade to it next week! Example:
C:\Temp>type meow.cpp
#include <algorithm>
#include <iostream>
#include <ostream>
#include <string>
#include <vector>
using namespace std;
int main() {
vector<string> v;
v.push_back("my");
v.push_back("cute");
v.push_back("fluffy");
v.push_back("kittens");
v.push_back("dislike");
v.push_back("puppies");
v.push_back("intensely");
const auto i = lower_bound(v.cbegin(), v.cend(), 7, [](const string& s,
string::size_type n) { return s.size() < n;
});
if (i == v.cend()) {
cout << "EPIC FAIL" << endl;
} else {
cout << "This should be kittens: " << *i << endl;
}
const auto j = partition_point(v.cbegin(), v.cend(), [](const string& s)
{ return s.size() < 6; });
if (j == v.cend()) {
cout << "FAIL OF AN EPIC NATURE" << endl;
} else {
cout << "This should be fluffy: " << *j << endl;
}
}
C:\Temp>cl /EHsc /nologo /W4 meow.cpp
meow.cpp
C:\Temp>meow
This should be kittens: kittens
This should be fluffy: fluffy
Stephan T. Lavavej
Visual C++ Libraries Developer
"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:eDmXqP20KHA.5996@TK2MSFTNGP05.phx.gbl...
Stephen Howe wrote:
Is it possible to use asymmetric functor predicates?
In practice, yes. Just write several overloads of operator()
vector <point2D> v2D;
sort (v2D.begin(), v2D.end(), CCompare2D());
Now if you had
bool DoesXExist(int x)
{
return binary_search(v2D.begin(), v2D.end(), x, SomeFunctor());
}
I can easily provide some conversion constructor that turns x into a
point2D
And then I can provide another functor that only processes just the x
portion of point2D
But what I would like to do is get away from having conversion
constructors at all.
I presume I would have some functor that is like so
class SomeFunctor
{
bool operator()(const point2D &e1, int x) const
{
return (e1.x < x);
}
bool operator()(int x, const point &e1) const
{
return (x < e1.x);
}
};
This is not well defined in the current standard. See
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#270
With most implementations (including the MSVC one), it would work if you
provide three overloads - the two you show and the third taking two
point2D's.
With C++0x, you would only need one overload of operator(), the one taking
point2D and int. binary_search and related algorithms no longer requre that
the sequence be sorted, but only that it be partitioned with respect to the
search key (that is, that e < x be true for some initial subsequence, and
false for everything else).
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not necessarily
a good idea. It is hard to be sure where they are going to land, and it
could be dangerous sitting under them as they fly overhead. -- RFC 1925