473,324 Members | 2,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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?

Jul 18 '07 #1
2 2391
On 18 Jul, 16:30, many_years_after <shua...@gmail.comwrote:
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
Jul 19 '07 #2
On Jul 19, 4:26 pm, dasjotre <dasjo...@googlemail.comwrote:
On 18 Jul, 16:30, many_years_after <shua...@gmail.comwrote:
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.
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:ja*********@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

Jul 20 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Tim Martin | last post by:
Hi, I came across a detail in "C++ Gotchas" by Stephen Dewhurst that confused me a bit. The author states: 'C++ has no "methods." Java and Smalltalk have methods. When you talk about an...
10
by: rg | last post by:
Hi all, I was wondering if anyone had dealt with a similar problem. I need to use a template function as the parameter for a particular function (also template function). The program compiles...
8
by: BekTek | last post by:
I have class that has member function that take two argument.. I'd like to use the member function to algorithm such as remove_if.. How can I bind that? class Foo{ void f(){ int a = 10;...
3
by: jjleto | last post by:
I have just learn about "pointer to member". Does someone have an actual example where "pointers to member" are usefull or necessary ? Can't always &(myClass.member) be used ? Thank you.
7
by: jolie.demiranda | last post by:
Dear all, I'm fairly new to C++ so forgive me is this topic may have been convered before... I'm having a problem with pointers to member functions. Essentially what I want to achieve is the...
2
by: waitan | last post by:
#include <algorithm> #include <iostream> #include <fstream> #include <string> #include <vector> #include <sstream> #include <iterator> #include <iomanip> using namespace std;
3
by: ryan.mitchley | last post by:
Hi all I have a class (cPort) that is designed to receive objects and, depending on the type, call a handler (callback) in any descendant of a cProcessBlock class. Callback functions take a...
13
by: JohnQ | last post by:
The implementation of classes with virtual functions is conceptually easy to understand: they use vtables. Which begs the question about POD structs: how are they associated with their member...
6
by: many_years_after | last post by:
Hi, cppers: I am studying cpp recently. As is said, member funciton can be as one parameter of stl algorithm. BUT when I pass member function whose parameter is instance of the class, it doesn't...
9
by: want.to.be.professer | last post by:
We know that alomost every algorithm function, such as for_each, find_if, use funcional as well as function pointer. But when I want to use another class's member function, how could I do? See...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.