473,383 Members | 1,788 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,383 software developers and data experts.

in-place functor comparison instead of std::binary_function

Hi all

Rather than writing std::binary:functions I thought it would be nice to
create a comparison expression directly into the algorithm expression.
Typical use is

struct Person
{
std::string name () const;
};

// Find "John"
std::vector<Person>::iterator it = std::find_if (persons.begin(),
persons.end(), somehow bind Person::name method == "John");

While std::equal could be used in some simple cases, it does not accept
mixed input like &Person::name and "John" (at least to my knowledge)

Is there a way to accomplish this type of comparison using a mix of
binding of functions /return values and plain values ?

Thanks,

Anders
Jan 30 '06 #1
2 2185
news.online.no wrote:
Hi all

Rather than writing std::binary:functions I thought it would be nice to
create a comparison expression directly into the algorithm expression.
Typical use is

struct Person
{
std::string name () const;
};

// Find "John"
std::vector<Person>::iterator it = std::find_if (persons.begin(),
persons.end(), somehow bind Person::name method == "John");

While std::equal could be used in some simple cases, it does not accept
mixed input like &Person::name and "John" (at least to my knowledge)

Is there a way to accomplish this type of comparison using a mix of
binding of functions /return values and plain values ?


boost::lambda provides a solution to your problem.

std::vector<Person>::iterator it = std::find_if (persons.begin(),
persons.end(), bind(&Person::name, _1) == "John");

it is perfectly suited for simple ad-hoc functor declarations in
std::algorithms and the like.

have a look at http://boost.org/doc/html/lambda.

-- peter

Jan 30 '06 #2
In article <bu********************@telenor.com>,
"news.online.no" <an********@geocap.no> wrote:
Hi all

Rather than writing std::binary:functions I thought it would be nice to
create a comparison expression directly into the algorithm expression.
Typical use is

struct Person
{
std::string name () const;
};

// Find "John"
std::vector<Person>::iterator it = std::find_if (persons.begin(),
persons.end(), somehow bind Person::name method == "John");

While std::equal could be used in some simple cases, it does not accept
mixed input like &Person::name and "John" (at least to my knowledge)

Is there a way to accomplish this type of comparison using a mix of
binding of functions /return values and plain values ?


compose1(bind2nd(equal_to<string>(), "John"), mem_fun_ref(&Person::name))

The above predicate takes a Person&, calls '.name()' on it, the passes
the result to a binder2nd which it to "John" using equal_to<string>.

Warning: compose1 is not part of the standard library, but is part of
the original STL. Look it up in <http://www.sgi.com/tech/stl/> for
example.

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Jan 30 '06 #3

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

Similar topics

0
by: Naren | last post by:
Hello Grp, I think I have understood little of this example,though the first_argument_type is not used,Can a reference to the derived class be one of base class members. And what is the use of it...
3
by: Martin Proulx | last post by:
Hello, I'd like to know how to correctly declare a member function so that it takes as parameter any function object, of which the operator() takes arguments of specific types. The idea is to...
2
by: Maitre Bart | last post by:
First, I describe my setup in 3 points. Then I describe what I want to do, and describe the error I get. 1) I have a container GTTable container pointer to data (class GT *). 2) I made a...
4
by: Steve | last post by:
I'll be the first to admit, I'm not entirely clear on the appropriate usage of either. From what I am reading in my books, a Struct and a Class are pretty much the same, with the difference being,...
5
by: ken.carlino | last post by:
Hi, I have a function which check if there is duplicate entries in a STL list: bool hasDuplicateY() { sort(_aList.begin(), _aList.end(), sort_y_comparator<A*>()); AList::iterator iter;
6
by: Dilip | last post by:
I have a vector of class object pointers that have 2 internal states (USED or EMPTY). USED is just a way of indicating the element in that slot has valid state. EMPTY means the element is...
2
by: Emmanuel Deloget | last post by:
Hello, I'm trying to find a (sfinae powered) way to verify if a particular type declares a subtype (either using typedef or by declaring a subclass). To be more concrete, let's say that I'm...
7
by: Alan | last post by:
Am I missing some sort of declaration or is there a syntax error in the definition of the binary function object "less_metric1" below? My compiler says: "ISO C++ forbids declaration of...
20
by: curious2007 | last post by:
I have the following program: #include "Vector.cpp" #include <iostream> using namespace std; template<class Arg1, class Arg2, class Result> struct binary_function
3
by: Bruintje Beer | last post by:
Hi, I am having the following question (see code below) the class Data is declared as class Data { public : // rest of class };
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.