473,325 Members | 2,342 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,325 software developers and data experts.

peek inside container's elements from UnaryPredicate

Hello,

Suppose I have a container of pair<int, int>, how would I use the
predefined function objects/adapters (e.g., greater, bind1st, mem_func,
compose_f_gx) and the "count" function from <algorithmto get and
number of pairs, "p", that have "p.first 5"?

Writing a custom UnaryPredicate function is easy, but I would
prefer to use the predefined ones if possible.

Example:

std::list<std::pair<int, int c;
// insert elements in c
int x = count(c.begin(), c.end(), /* UnaryPredicate */);
// x is the number of pairs, p, with p.first 5

More general, how can I compose the predefined function
objects/adapters, so that the resulting UnaryPredicate peeks inside the
elements of a container?

Thanks,
Rares
Jul 5 '08 #1
1 1225
In article <y1*************@ics.uci.edu>,
Rares Vernica <ra***@ics.uci.eduwrote:
Hello,

Suppose I have a container of pair<int, int>, how would I use the
predefined function objects/adapters (e.g., greater, bind1st, mem_func,
compose_f_gx) and the "count" function from <algorithmto get and
number of pairs, "p", that have "p.first 5"?

Writing a custom UnaryPredicate function is easy, but I would
prefer to use the predefined ones if possible.
The standard doesn't have compose_f_gx/compose1 or select1st.
Example:

std::list<std::pair<int, int c;
// insert elements in c
int x = count(c.begin(), c.end(), /* UnaryPredicate */);
// x is the number of pairs, p, with p.first 5
You have to use count_if, not count.

int x = count_if(c.begin(), c.end(),
compose1(bind2nd(greater<int>(), 5), select1st<pair<int,int()));
More general, how can I compose the predefined function
objects/adapters, so that the resulting UnaryPredicate peeks inside the
elements of a container?
template < typename Pair >
struct select1st :
public std::unary_function< Pair, typename Pair::first_type >
{
const typename Pair::first_type& operator()( const Pair& x ) const {
return x.first;
}

typename Pair::first_type& operator()( Pair& x ) const {
return x.first;
}
};
Jul 5 '08 #2

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

Similar topics

1
by: Wolfgang Lipp | last post by:
my question is: do we need container elements for repeating elements in data-centric xml documents? or is it for some reason very advisable to introduce containers in xml documents even where not...
2
by: Ney André de Mello Zunino | last post by:
Hello. The issue is quite known: you have a block-level container holding a set of floated elements and you need that the container's content height take the floated elements' dimensions into...
8
by: Nobody | last post by:
The requirement that STL container elements have to be assignable is causing me a problem. Consider a class X which contains both const and non-const data members: class X { public: X(const...
1
by: charlie | last post by:
I started out with table based page and moved to a managed absolute position and now I've been trying to do the project again. Here is a link to the absolute position page. ...
4
by: Manfred Braun | last post by:
Hi All ! I think, there is a bug in the System.Console class related to use the STDIO streams. I am doing a very simple thing in a console-based program named CS Console.In.Peek(); and...
4
by: Rares Vernica | last post by:
Hi, How can I save a reference inside a container? For example I have: map<string, unsignedX; I would like to be able to save a reference to a position inside X. For a vector, the...
18
by: Goran | last post by:
Hi @ all! Again one small question due to my shakiness of what to use... What is better / smarter? private: vector<MyClass_t* itsVector; OR...
2
by: Terry Reedy | last post by:
Luis Zarrabeitia wrote: Interesting observation. Iterators are intended for 'iterate through once and discard' usages. To zip a long sequence with several short sequences, either use...
5
by: Olumide | last post by:
Hello - To begin with, I'm not the most experienced STL use out there but I'm slowly getting there. One bug(?) that's plagued me of recent is the way the OS (or is it STL?) appears to relocate a...
0
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...
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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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.