Thomas Sampson

STL algorithm predicates

Leave a comment

Just a quick reminder on how to set up a predicate function for an stl algorithm (accumulate shown here) using the () operator on a struct/class.

This arbitrary example accumulates all integer values in bag which are greater than 2. There third parameter to accumulate (the zero) means start accumulating from 0 rather than a given constant.

struct check
{
	int operator()(int l, int r)
	{
		return (r>2)?  (l+r): l;
	}
};

int total=std::accumulate(bag.begin(),bag.end(),0,check());
Advertisements

Author: tomtech999

I have recently graduated with a 1st class degree in MComp Games Software Development at Sheffield Hallam University, focusing primarily on application development in C++, with experience in graphics programming, scripting languages, DVCS/VCS and web technology. In my spare time I enjoy Drumming, Reading and Snowboarding!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s