473,732 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<<(ostr eam& 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(co nst char* s)
{
cout << s << " " << x;
}
};
int main()
{
vector<Pointvec ;

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

for_each(
vec.begin(),
vec.end(),
bind2nd(mem_fun _ref(&Point::pr intWithPre),"he llo:")
); // OK
for_each(
vec.begin(),
vec.end(),
mem_fun_ref(&Po int::print)); // OK
sort(vec.begin( ), vec.end(), mem_fun_ref(&Po int::LargeThan) ); //
ERROR
return 0;
}

what is the reason?

Jul 18 '07 #1
2 2434
On 18 Jul, 16:30, many_years_afte r <shua...@gmail. comwrote:
for_each(
vec.begin(),
vec.end(),
mem_fun_ref(&Po int::print)); // OK
sort(vec.begin( ), vec.end(), mem_fun_ref(&Po int::LargeThan) ); //
ERROR

return 0;

}

what is the reason?
std::sort takes binary predicate as third argument
mem_fun_ref(&Po int::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...@googl email.comwrote:
On 18 Jul, 16:30, many_years_afte r <shua...@gmail. comwrote:
for_each(
vec.begin(),
vec.end(),
mem_fun_ref(&Po int::print)); // OK
sort(vec.begin( ), vec.end(), mem_fun_ref(&Po int::LargeThan) ); //
ERROR
return 0;
}
what is the reason?

std::sort takes binary predicate as third argument
mem_fun_ref(&Po int::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 objektorientier ter Datenverarbeitu ng
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
8384
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 object-oriented design and are feeling particularly pretentious, you may use the terms "message" and "method," but when you get down to discussing a C++ implementation of your design, use the terms "function call" and "member function."'
10
1786
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 into an object file but then at the final stage it says that it can't find template function. The platform is WindowsXP Pro, MSCV++ ..Net. More specifically what I want to do is write an atl algorithm that will also
8
3403
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; remove_if(list.begin(), list.end(), HERE);
3
1366
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
2177
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 following: class A { public: double (A::*ptrToFunction)(double); }
2
2955
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
3060
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 shared_ptr<cBaseas a parameter, and return void. The code was working fine, although I have encountered problems (under a Microsoft compiler, of course - VC 8.0) when I attempt to add callbacks to a class with multiple inheritance. I hate multiple
13
2529
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 functions in common implementations? And where is the 'this' ptr tucked away at for POD structs with member functions? John
6
448
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 work. For example: class Point { public: int x; int y;
9
2099
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 example: class TestPrint { public:
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9447
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9307
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9235
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9181
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.