M
many_years_after
hi, cppers:
It is said that member function can be as parameter of STL algorithm.
BUT when I pass member function whose parameter is a instance of the
class, it dose not work.
class Point
{
public:
int x; int y;
Point(int xx, int yy)
{
x = xx;
y = yy;
}
friend ostream& operator<<(ostream& out,const Point& p)
{
out << p.x << " "<< p.y << endl;
return out;
}
bool LargeThan(const Point& p)
{
return (x > p.x)|| ((x==p.x) && (y > p.y));
}
void print()
{
cout << x << " " << y;
}
void printWithPre(const char* s)
{
cout << s << " " << x;
}
};
int main()
{
vector<Point> vec;
for (int i = 0; i < 10; i++)
vec.push_back(Point(i, i));
for_each(
vec.begin(),
vec.end(),
bind2nd(mem_fun_ref(&Point:rintWithPre),"hello:")
); // OK
for_each(
vec.begin(),
vec.end(),
mem_fun_ref(&Point:rint)); // OK
sort(vec.begin(), vec.end(), mem_fun_ref(&Point::LargeThan)); //
ERROR
return 0;
}
what is the reason?
It is said that member function can be as parameter of STL algorithm.
BUT when I pass member function whose parameter is a instance of the
class, it dose not work.
class Point
{
public:
int x; int y;
Point(int xx, int yy)
{
x = xx;
y = yy;
}
friend ostream& operator<<(ostream& out,const Point& p)
{
out << p.x << " "<< p.y << endl;
return out;
}
bool LargeThan(const Point& p)
{
return (x > p.x)|| ((x==p.x) && (y > p.y));
}
void print()
{
cout << x << " " << y;
}
void printWithPre(const char* s)
{
cout << s << " " << x;
}
};
int main()
{
vector<Point> vec;
for (int i = 0; i < 10; i++)
vec.push_back(Point(i, i));
for_each(
vec.begin(),
vec.end(),
bind2nd(mem_fun_ref(&Point:rintWithPre),"hello:")
); // OK
for_each(
vec.begin(),
vec.end(),
mem_fun_ref(&Point:rint)); // OK
sort(vec.begin(), vec.end(), mem_fun_ref(&Point::LargeThan)); //
ERROR
return 0;
}
what is the reason?