Connecting Tech Pros Worldwide Help | Site Map

Member function as parameter of STL algorithm

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 18th, 2007, 03:35 PM
many_years_after
Guest
 
Posts: n/a
Default Member function as parameter of STL algorithm

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<Pointvec;

for (int i = 0; i < 10; i++)
vec.push_back(Point(i, i));

for_each(
vec.begin(),
vec.end(),
bind2nd(mem_fun_ref(&Point::printWithPre),"hello:" )
); // OK


for_each(
vec.begin(),
vec.end(),
mem_fun_ref(&Point::print)); // OK
sort(vec.begin(), vec.end(), mem_fun_ref(&Point::LargeThan)); //
ERROR


return 0;
}

what is the reason?


  #2  
Old July 19th, 2007, 02:35 PM
dasjotre
Guest
 
Posts: n/a
Default Re: Member function as parameter of STL algorithm

On 18 Jul, 16:30, many_years_after <shua...@gmail.comwrote:
Quote:
for_each(
vec.begin(),
vec.end(),
mem_fun_ref(&Point::print)); // OK
sort(vec.begin(), vec.end(), mem_fun_ref(&Point::LargeThan)); //
ERROR
>
return 0;
>
}
>
what is the reason?
std::sort takes binary predicate as third argument
mem_fun_ref(&Point::LargeThan) returns unary
function that takes reference to a Point object.


regards

DS


  #3  
Old July 20th, 2007, 08:35 AM
James Kanze
Guest
 
Posts: n/a
Default Re: Member function as parameter of STL algorithm

On Jul 19, 4:26 pm, dasjotre <dasjo...@googlemail.comwrote:
Quote:
On 18 Jul, 16:30, many_years_after <shua...@gmail.comwrote:
>
Quote:
for_each(
vec.begin(),
vec.end(),
mem_fun_ref(&Point::print)); // OK
sort(vec.begin(), vec.end(), mem_fun_ref(&Point::LargeThan)); //
ERROR
Quote:
Quote:
return 0;
Quote:
Quote:
}
Quote:
Quote:
what is the reason?
>
std::sort takes binary predicate as third argument
mem_fun_ref(&Point::LargeThan) returns unary
function that takes reference to a Point object.
No. In this case, mem_fun_ref returns a binary functional
object that takes a reference to a reference as its second
argument. It doesn't compile because the language doesn't allow
references to references.

The function adaptors in the original standard are seriously
broken, and close to useless in practice. Boost has solutions
to this; they were adopted in TR1, and will be part of the next
version of the standard. So the "correct" answer here is to
forget about mem_fun_ref, and use the Boost stuff.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.