Page 1 of 1

C++ STL BINARY PREDICATE FUNCTION TUTORIAL C++ STL BINARY PREDICATE FUNCTION TUTORIAL Rate Topic: 1 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=120595&amp;s=021a334c0347f9516f456db1892fb57e&md5check=' + ipb.vars['secure_hash'], cur_rating: 5, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 Elcric

• D.I.C Regular

Reputation: 102
• Posts: 453
• Joined: 02-May 09

Posted 16 August 2009 - 08:49 PM

C++ STL BINARY PREDICATE FUNCTION TUTORIAL

WHAT YOU WILL LEARN IN THIS TUTORIAL:
1. You will learn how to write your own binary predicates.
2. You will learn how to use binary predicate functions with arrays.
3. You will learn how to use binary predicate functions with lists.
4. You will learn how to use binary predicate functions with maps.
5. You will learn how to use binary predicate functions with sets.
6. You will learn how to use binary predicate functions with vectors.

• I. INTRODUCTION

Hello; nice to meet you! Welcome to the “C++ STL Binary Predicate Function Tutorial.”

• II. BINARY PREDICATE FUNCTIONS

When a unary function object returns a bool, it is called a predicate. When a binary function object returns a bool it is called a binary predicate. A binary predicate is a function object that takes two arguments, bool bP(arg1, arg2){…}, performs a comparison using those two arguments and returns a bool value true if the condition is satisfied, false if it is not satisfied. A function object that combines two function objects is called an adaptive function object.

• III. WRITING YOUR OWN BINARY PREDICATES

Use #include <functional> when you plan to write binary predicates.

Algorithms are often used in combination with binary predicates; therefore, also use #include <algorithm> when you plan to write binary predicates.

You do not always have to create your own binary predicate. First try and use one of the library binary predicates. Using a library binary predicate reduces the chances of error and improves portability.

The Standard Template Library (STL) provides the following common binary predicates:

1. equal_to(){ if (arg1 == arg2) return TRUE; else return FALSE;}
2. not_equal_to(){if (arg1 != arg2) return TRUE; else return FALSE;}
3. greater(){if (arg1 > arg2) return TRUE; else return FALSE;}
4. less(){if (arg1 < arg2) return TRUE; else return FALSE;}
5. greater_equal(){if (arg1 >= arg2) return TRUE; else return FALSE;}
6. less_equal(){if (arg1 <= arg2) return TRUE; else return FALSE;}
7. logical_and(){if (arg1 && arg2) return TRUE; else return FALSE;}
8. logical_or{ if (arg1 || arg2) return TRUE; else return FALSE;}

The STL provides the following common binary arithmetic function objects:

1. plus(){ return ( arg1 + arg2); }
2. minus(){ return ( arg1 - arg2 ); }
3. multiplies(){ return ( arg1 * arg2 ); }
4. divides(){ return ( arg1 / arg2 ); }
5. modulus(){ return ( arg1 % arg2 ); }

Use the STL binary predicates and binary arithmetic function objects as examples; and, take into consideration the following when writing your own binary predicates:

1. A binary predicate is called like a function; therefore, a binary predicate must be callable.

2. A binary predicate must accept two arguments and return a value that is convertible to Boolean. Two arguments are supplied when the predicate is called. The return value is used as a conditional expression and must be convertible to type bool.

3. The binary predicate must not modify its arguments. A binary predicate must not modify the elements in the input sequence that it receives as arguments. The binary predicate can inspect the sequence elements, but it must not modify them.

4. Binary predicates logic must not be dependent on the order in which sequence elements are supplied to it.

5. Write binary predicates that return either true or false on the basis of a comparison that is not case sensitive.

6. Function objects are more useful when implemented in a struct or a class.

A struct implements operator(). Such objects that double as functions are known as function objects or functors. Unary and binary predicates are implemented using function objects.

The implementation of the function object can be contained by the operator() of a class or a struct. struct members are public by default. The power of a struct becomes apparent when it is used to store information. A struct provides the added benefit of being able to contain state-related information.

• IV. ARRAY EXAMPLE

The syntax for instantiating an array object is as follows:

type arrayName [arrayElements];

The attached array example program does the following:

1. First, the initialized array elements are displayed.

2. Second, the random_shuffle() function randomly re-orders the initialized array elements.

3. Third, the binary predicate function displays the array in descending order, high to low.

Attached is the array example program:

```//**************************************
//C++ STL BINARY PREDICATE FUNCTIONS
//TUTORIAL
//Array Example:
//**************************************
#ifndef CALC_ERROR_H
#define CALC_ERROR_H
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <sstream>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#endif  //CALC_ERROR_H

void ClearScreen();				//Clears the screen.
void Pause();					  //Waits for user response.

//************************************
//Clear Screen.
//************************************
void ClearScreen()
{
system( "CLS" );
cout << endl << endl << endl;
}

//************************************
//Pause.
//************************************
void Pause()
{
cout << endl << endl << endl;
cout << "  ";
system( "PAUSE" );
}

//************************************
// MAIN() FUNCTION.
//************************************
int main(int argc, char* argv[])
{
const int SIZE = 51;
const int WIDTH = 6;
ClearScreen();
cout << "  C++ STL BINARY PREDICATE FUNCTIONS TUTORIAL: ARRAY EXAMPLE" << endl;
cout << "  -----------------------------------------------------------" << endl;
cout << "  The initialized element values of the array are as follows:." << endl << endl;

cout << "  " << ( SIZE - 1 ) << " array numbers displayed left to right in initial ascending order. " << endl;
int myArray[ ( SIZE - 1 ) ];
int i;
for (i = 0; i < ( SIZE - 1 ); ++i)
{
myArray[i] = i;
if ( i == 0 )
{
cout << "  ";
}
cout << setw(WIDTH) <<  myArray[i] << " ";
if (( i == 9 ) || ( i == 19 ) || ( i == 29 ) || ( i == 39 ) )
{
cout << endl << "  ";
}
}
cout << endl << "  ";
Pause();

ClearScreen();
cout << "  C++ STL BINARY PREDICATE FUNCTION TUTORIAL: ARRAY EXAMPLE" << endl;
cout << "  -----------------------------------------------------------" << endl;
cout << "  This example uses a random_shuffle function to sort the array." << endl << endl;
cout << "  " << ( SIZE - 1 )<< " numbers randomly shuffled by random_shuffle function" << endl;
cout << "  and displayed left to right." << endl << endl;

random_shuffle(&myArray[0], &myArray[ ( SIZE - 1 ) ]);

for (i = 0; i < ( SIZE - 1 ); ++i)
{
if ( i == 0 )
{
cout << "  ";
}
cout << setw(WIDTH) <<  myArray[i] << " ";
if (( i == 9 ) || ( i == 19 ) || ( i == 29 ) || ( i == 39 ) )
{
cout << endl << "  ";
}
}
cout << endl << "  ";
Pause();

ClearScreen();
cout << "  C++ STL BINARY PREDICATE FUNCTION TUTORIAL: ARRAY EXAMPLE" << endl;
cout << "  -----------------------------------------------------------" << endl;
cout << "  This example uses a binary predicate to sort the array." << endl << endl;
cout << "  " << ( SIZE - 1 )
<< " numbers sorted left to right in descending order." << endl;
cout << endl << endl;
sort(&myArray[0], &myArray[ ( SIZE - 1 ) ], greater<int>());

for (i = 0; i < ( SIZE - 1 ); ++i)
{
if ( i == 0 )
{
cout << "  ";
}
cout << setw(WIDTH) <<  myArray[i] << " ";
if (( i == 9 ) || ( i == 19 ) || ( i == 29 ) || ( i == 39 ) )
{
cout << endl << "  ";
}
}
cout << endl << "  ";
Pause();

//Greater is a binary function object.
//Its operator() returns true if x is greater than y.
//You can pass a greater object to any algorithm that
//requires a binary function.

return EXIT_SUCCESS;
}

```

• V. LIST EXAMPLE:

The syntax for instantiating a list object is as follows:

list <type> objectName;

list is a C++ keyword. The angle brackets contain the word type <type> which is a placeholder for the type of the list you are instantiating. objectName is the name of the list object you are instantiating.

The attached list example program does the following:

1. First, the list is initialized and displayed.

2. Second, the list is sorted in descending, order using a binary predicate, and displayed.

Attached is the list example program:

```//**************************************
//C++ STL BINARY PREDICATE FUNCTIONS
//TUTORIAL
//List Example:
//***************************************
#ifndef CALC_ERROR_H
#define CALC_ERROR_H
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <set>
#include <sstream>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#endif  //CALC_ERROR_H

void ClearScreen();												   //Clears the screen.
void Pause();														 //Waits for user response.
void displayList( const list<int>& OList );						   //Displays list.
bool BinarySortPredicateDescending( const int& lsh, const int& rsh );  //Binary Sort Predicate

//************************************
//Clear Screen.
//************************************
void ClearScreen()
{
system( "CLS" );
cout << endl << endl << endl;
}

//************************************
//Pause.
//************************************
void Pause()
{
cout << endl << endl << endl;
cout << "  ";
system( "PAUSE" );
cout << endl << endl << endl;
}

//************************************
//Displays list.
//************************************
void displayList( const list<int>& OList )
{
//Instantiates object myItr iterator
list< int >::const_iterator myIt;
const int WIDTH = 6;
int lineCounter = 0;
for ( myIt = OList.begin(); myIt != OList.end(); ++myIt )
{
if ( lineCounter == 0 )
{
cout << "  ";
}
cout << setw(WIDTH) <<  *myIt << " ";
if (( lineCounter ==  9 ) ||
( lineCounter == 19 ) ||
( lineCounter == 29 ) ||
( lineCounter == 39 ) )
{
cout << endl << "  ";
}

++lineCounter;
}
}

//************************************
//Binary Predicate
//************************************
bool BinarySortPredicateDescending( const int& lsh, const int& rsh )
{
return ( lsh > rsh );
}

//************************************
// MAIN() FUNCTION.
//************************************
int main(int argc, char* argv[])
{
const int SIZE = 22;
ClearScreen();
cout << "   C++ STL BINARY PREDICATE FUNCTIONS TUTORIAL: LIST EXAMPLE   " << endl;
cout << "  -----------------------------------------------------------  " << endl<< endl;
cout << "  First, the list is push_back initialized in ascending order, " << endl;
cout << "  low to high, and displayed.								  " << endl << endl;

//Instantiates object OList of class type list.
list< int >OList;

//Initializes list.
for (int i = 0; i < ( SIZE - 1 ); ++i)
{
OList.push_back( i );
}

displayList( OList );

Pause();

ClearScreen();
cout << "   C++ STL BINARY PREDICATE FUNCTIONS TUTORIAL: LIST EXAMPLE   " << endl;
cout << "  -----------------------------------------------------------  " << endl<< endl;
cout << "  Second, the list is sorted in descending order, high to low, " << endl;
cout << "  using a binary predicate and displayed.					  " << endl << endl;

OList.sort ( BinarySortPredicateDescending );
displayList( OList );

Pause();
return EXIT_SUCCESS;
}

```

• VI. MAP EXAMPLE:

A map is a key-value pair container. You use the key to lookup map elements. A map can only store unique keys. Map elements are sorted on insertion with a default ascending sort order, lower to higher. The syntax for instantiating a map object is as follows:

map <keyType, valueType, BinaryPredicate> objectName;

map is a C++ keyword. keyType is the type of the key your map will use. valueType is the type of the value your map will use. The binary predicate is optional and when used overrides the default ascending less than < sort mechanism on insertion. objectName is the name you pick for the map class object you are creating.

The attached map example program does the following:

When you examine the code in the attached map example program you will see the sequence of the data used to initialize the map was shuffled prior to insertion into the map. A map sorts in ascending order on insertion by default. The sort on insertion functionality of the map was overridden with a sort predicate as is proven by the display which shows the map elements after insertion in key-value pair descending order, high to low.

Attached is the map example program:

```//**************************************
//C++ STL BINARY PREDICATE FUNCTIONS
//TUTORIAL
//Map Example:
//***************************************
#ifndef CALC_ERROR_H
#define CALC_ERROR_H
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <set>
#include <sstream>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#endif  //CALC_ERROR_H

//************************************
//Class CC
//************************************
class CC
{
private:

int Number;							  //Temperature equation number.
string Equation;						 //Temperature conversion equation.

public:

CC()									 //Constructor used when neither equation number nor conversion equation are entered.
{
Number = 0;
Equation = " ";
}

CC(int n)								//Constructor used when only conversion equation number is entered.
{
Number = n;						  //n is the temperature conversion equation number.
Equation = " ";

}

CC(int n, string e)					  //Constructor used when both equation number and conversion equation are entered.
{
Number = n;
Equation = e;						//e is the temperature conversion equation.

}

int m_getNumber();					   //Temperature conversion equation number.
string m_getEquation();				  //Temperature conversion equation.
~CC()								//Destructor
{

}
};

void ClearScreen();							  //Clears the screen.
void Pause();
bool operator < (CC p1, CC p2);				  //Sort predicate.

//************************************
//Clear Screen.
//************************************
void ClearScreen()
{
system( "CLS" );
cout << endl << endl << endl;
}

//************************************
//Pause.
//************************************
void Pause()
{
cout << endl << endl << endl;
cout << "  ";
system( "PAUSE" );
cout << endl << endl << endl;
}

//************************************
//Gets the temperature conversion
//equation number.
//************************************
int CC::m_getNumber()
{
return Number;
}

//************************************
//Gets the temperature conversion
//equation.
//************************************
string CC::m_getEquation()
{
//cout << endl << endl;
//cout << "  86  string m_getEquation()." << endl;

return Equation;
}

//************************************
//Binary Predicate
//This function sorts the Map.
//************************************
struct DescendingOrder
{
bool operator () (CC p1, CC p2)
{
return   p1.m_getNumber() > p2.m_getNumber();
};
};
//************************************
// MAIN() FUNCTION.
//************************************
int main(int argc, char* argv[])
{
//Instantiates object OMap of class type map.
map< int, string, DescendingOrder >OMap;

OMap.insert(map< int, string >::value_type (37, "From Celsius To Kelvin		= ((FromTemp) + (273.15))"));
OMap.insert(map< int, string >::value_type (38, "From Centigrade To Kelvin	 = ((FromTemp) + (273.15))"));
OMap.insert(map< int, string >::value_type (39, "From Delisle to Kelvin		= (373.15 - ((FromTemp * 2) / 3))"));
OMap.insert(map< int, string >::value_type (40, "From Fahrenheit To Kelvin	 = (((FromTemp + 459.67) *5) /9)"));
OMap.insert(map< int, string >::value_type (41, "From Kelvin To Kelvin		 = FromTemp"));
OMap.insert(map< int, string >::value_type (42, "From Newton To Kelvin		 = (((FromTemp * 100) / 33) + 273.15)"));
OMap.insert(map< int, string >::value_type (43, "From Rankine To Kelvin		= ((FromTemp * 5) / 9)"));
OMap.insert(map< int, string >::value_type (44, "From Reaumur To Kelvin		= (((FromTemp * 5) / 4) +  273.15)"));
OMap.insert(map< int, string >::value_type (45, "From Romer To Kelvin		  = ((((FromTemp - 7.5) * 40) / 21) + 273.15)"));
OMap.insert(map< int, string >::value_type (46, "From Celsius To Newton		= ((FromTemp * 33) / 100)"));
OMap.insert(map< int, string >::value_type (47, "From Centigrade To Newton	 = ((FromTemp * 33) / 100)"));
OMap.insert(map< int, string >::value_type (48, "From Delisle to Newton		= (33 - ((FromTemp * 11) / 50))"));
OMap.insert(map< int, string >::value_type (49, "From Fahrenheit To Newton	 = (((FromTemp - 32) * 11) / 60)"));
OMap.insert(map< int, string >::value_type (50, "From Kelvin To Newton		 = (((FromTemp - 273.15) * 33) / 100)"));
OMap.insert(map< int, string >::value_type (51, "From Newton To Newton		 = FromTemp"));
OMap.insert(map< int, string >::value_type (52, "From Rankine To Newton		= (((FromTemp - 491.67) * 11) / 60)"));
OMap.insert(map< int, string >::value_type (53, "From Reaumur To Newton		= ((FromTemp * 33) / 80)"));
OMap.insert(map< int, string >::value_type (54, "From Romer To Newton		  = (((FromTemp - 7.5) * 22) / 35)"));
OMap.insert(map< int, string >::value_type (55, "From Celsius To Rankine	   = (((FromTemp * 9) / 5) + (491.67))"));
OMap.insert(map< int, string >::value_type (56, "From Centigrade To Rankine	= (((FromTemp * 9) / 5) + (491.67))"));
OMap.insert(map< int, string >::value_type (57, "From Delisle to Rankine	   = (671.67 - ((FromTemp * 6) / 5))"));
OMap.insert(map< int, string >::value_type (58, "From Fahrenheit To Rankine	= (FromTemp + 459.67)"));
OMap.insert(map< int, string >::value_type (59, "From Kelvin To Rankine		= ((FromTemp * 9) / 5)"));
OMap.insert(map< int, string >::value_type (60, "From Newton To Rankine		= (((FromTemp * 60) / 11) + 491.67)"));
OMap.insert(map< int, string >::value_type (61, "From Rankine To Rankine	   = FromTemp"));
OMap.insert(map< int, string >::value_type (62, "From Reaumur To Rankine	   = (((FromTemp * 9) / 4) + 491.67)"));
OMap.insert(map< int, string >::value_type (63, "From Romer To Rankine		 = ((((FromTemp - 7.5) * 24) / 7) + 491.67)"));
OMap.insert(map< int, string >::value_type (1, "From Celsius To Celsius	   = FromTemp"));
OMap.insert(map< int, string >::value_type (2, "From Centigrade To Celsius	= FromTemp"));
OMap.insert(map< int, string >::value_type (3, "From Delisle to Celsius	   = (100 - ((FromTemp * 2) / 3))"));
OMap.insert(map< int, string >::value_type (4, "From Fahrenheit To Celsius	= (((FromTemp - 32) * 5) / 9)"));
OMap.insert(map< int, string >::value_type (5, "From Kelvin To Celsius		= (FromTemp - 273.15)"));
OMap.insert(map< int, string >::value_type (6, "From Newton To Celsius		= ((FromTemp * 100) / 33)"));
OMap.insert(map< int, string >::value_type (7, "From Rankine To Celsius	   = (((FromTemp - 491.67) * 5) / 9)"));
OMap.insert(map< int, string >::value_type (8, "From Reaumur To Celsius	   = ((FromTemp * 5) / 4)"));
OMap.insert(map< int, string >::value_type (9, "From Romer To Celsius		 = (((FromTemp - 7.5) * 40) / 21)"));
OMap.insert(map< int, string >::value_type (10, "From Celsius To Centigrade	= FromTemp"));
OMap.insert(map< int, string >::value_type (11, "From Centigrade To Centigrade = FromTemp"));
OMap.insert(map< int, string >::value_type (12, "From Delisle to Centigrade	= (100 - ((FromTemp * 2) / 3))"));
OMap.insert(map< int, string >::value_type (13, "From Fahrenheit To Centigrade = (((FromTemp - 32) * 5) / 9)"));
OMap.insert(map< int, string >::value_type (14, "From Kelvin To Centigrade	 = (FromTemp - 273.15)"));
OMap.insert(map< int, string >::value_type (15, "From Newton To Centigrade	 = ((FromTemp * 100) / 33)"));
OMap.insert(map< int, string >::value_type (16, "From Rankine To Centigrade	= (((FromTemp - 491.67) * 5) / 9)"));
OMap.insert(map< int, string >::value_type (17, "From Reaumur To Centigrade	= ((FromTemp * 5) / 4)"));
OMap.insert(map< int, string >::value_type (18, "From Romer To Centigrad	   = (((FromTemp - 7.5) * 40) / 21)"));
OMap.insert(map< int, string >::value_type (19, "From Celsius To Delisle	   = (((100 - FromTemp) * 3) / 2)"));
OMap.insert(map< int, string >::value_type (20, "From Centigrade To Delisle	= (((100 - FromTemp) * 3) / 2)"));
OMap.insert(map< int, string >::value_type (21, "From Delisle to Delisle	   = FromTemp"));
OMap.insert(map< int, string >::value_type (22, "From Fahrenheit To Delisle	= (((212 - FromTemp) * 5) / 6)"));
OMap.insert(map< int, string >::value_type (23, "From Kelvin To Delisle		= (((373.15 - FromTemp) * 3) / 2)"));
OMap.insert(map< int, string >::value_type (24, "From Newton To Delisle		= (((33 - FromTemp) * 50) / 11)"));
OMap.insert(map< int, string >::value_type (25, "From Rankine To Delisle	   = (((671.67 - FromTemp) * 5) / 6)"));
OMap.insert(map< int, string >::value_type (26, "From Reaumur To Delisle	   = (((80 - FromTemp) * 15) / 8)"));
OMap.insert(map< int, string >::value_type (27, "From Romer To Delisle		 = (((60 - FromTemp) * 20) / 7)"));
OMap.insert(map< int, string >::value_type (28, "From Celsius To Fahrenheit	= (((FromTemp * 9) / 5) + 32)"));
OMap.insert(map< int, string >::value_type (29, "From Centigrade To Fahrenheit = (((FromTemp * 9) / 5) + 32)"));
OMap.insert(map< int, string >::value_type (30, "From Delisle to Fahrenheit	= (212 - ((FromTemp * 6) / 5))"));
OMap.insert(map< int, string >::value_type (31, "From Fahrenheit To Fahrenheit = FromTemp"));
OMap.insert(map< int, string >::value_type (32, "From Kelvin To Fahrenheit	 = (((FromTemp * 9) / 5) - 459.67)"));
OMap.insert(map< int, string >::value_type (33, "From Newton To Fahrenheit	 = (((FromTemp * 60) / 11) + 32)"));
OMap.insert(map< int, string >::value_type (34, "From Rankine To Fahrenheit	= (FromTemp - 459.67)"));
OMap.insert(map< int, string >::value_type (35, "From Reaumur To Fahrenheit	= (((FromTemp * 9) / 4) + 32)"));
OMap.insert(map< int, string >::value_type (36, "From Romer To Fahrenheit	  = ((((FromTemp - 7.5) * 24) / 7) + 32)"));
OMap.insert(map< int, string >::value_type (64, "From Celsius To Reaumur	   = ((FromTemp * 4) / 5)"));
OMap.insert(map< int, string >::value_type (65, "From Centigrade To Reaumur	= ((FromTemp * 4) / 5)"));
OMap.insert(map< int, string >::value_type (66, "From Delisle to Reaumur	   = (80 - ((FromTemp * 8) / 15))"));
OMap.insert(map< int, string >::value_type (67, "From Fahrenheit To Reaumur	= (((FromTemp - 32) * 4) / 9)"));
OMap.insert(map< int, string >::value_type (68, "From Kelvin To Reaumur		= (((FromTemp - 273.15) * 4) / 5)"));
OMap.insert(map< int, string >::value_type (69, "From Newton To Reaumur		= ((FromTemp * 80) / 33)"));
OMap.insert(map< int, string >::value_type (70, "From Rankine To Reaumur	   = (((FromTemp - 491.67) * 4) / 9)"));
OMap.insert(map< int, string >::value_type (71, "From Reaumur To Reaumur	   = FromTemp"));
OMap.insert(map< int, string >::value_type (72, "From Romer To Reaumur		 = (((FromTemp - 7.5) * 32) / 21)"));
OMap.insert(map< int, string >::value_type (73, "From Celsius To Romer		 = (((FromTemp * 21) / 40) + 7.5)"));
OMap.insert(map< int, string >::value_type (74, "From Centigrade To Romer	  = (((FromTemp * 21) / 40) + 7.5)"));
OMap.insert(map< int, string >::value_type (75, "From Delisle to Romer		 = (60 - ((FromTemp * 7) / 20))"));
OMap.insert(map< int, string >::value_type (76, "From Fahrenheit To Romer	  = ((((FromTemp - 32) * 7) / 24) + 7.5)"));
OMap.insert(map< int, string >::value_type (77, "From Kelvin To Romer		  = ((((FromTemp - 273.15) * 21) / 40) + 7.5)"));
OMap.insert(map< int, string >::value_type (78, "From Newton To Romer		  = (((FromTemp * 35) / 22) + 7.5)"));
OMap.insert(map< int, string >::value_type (79, "From Rankine To Romer		 = ((((FromTemp - 491.67) * 7) / 24) + 7.5)"));
OMap.insert(map< int, string >::value_type (80, "From Reaumur To Romer		 = (((FromTemp * 21) / 32) + 7.5)"));
OMap.insert(map< int, string >::value_type (81, "From Romer To Romer		   = FromTemp\n"));

int counter = 0;
cout << endl << endl << endl;
//Display contents of the map.
map< int, string, DescendingOrder >::const_iterator iPairLocator;
cout << "	C++ STL BINARY PREDICATE FUNCTIONS TUTORIAL: MAP EXAMPLE	  " << endl;
cout << "  ------------------------------------------------------------	" << endl<< endl;
cout << "  The map is initialized using shuffled data and displayed using: " << endl;
cout << "  Key Type: int, Value Type: string, and a binary predicate	   " << endl;
cout << "  which overides the default ascending order and displays in	  " << endl;
cout << "  descending order, high to low.								  " << endl << endl << endl << endl<< endl;
for ( iPairLocator = OMap.begin(); iPairLocator != OMap.end(); ++iPairLocator)
{
cout << "  " << iPairLocator->first;
cout << " " << iPairLocator->second << endl << endl;
counter++;
if ((counter ==  9) ||
(counter == 18) ||
(counter == 27) ||
(counter == 36) ||
(counter == 45) ||
(counter == 54) ||
(counter == 63) ||
(counter == 72))
{
Pause();
ClearScreen();
cout << "	C++ STL BINARY PREDICATE FUNCTIONS TUTORIAL: MAP EXAMPLE	  " << endl;
cout << "  ------------------------------------------------------------	" << endl<< endl;
cout << "  The map is initialized using shuffled data and displayed using: " << endl;
cout << "  Key Type: int, Value Type: string, and a binary predicate	   " << endl;
cout << "  which overides the default ascending order and displays in	  " << endl;
cout << "  descending order, high to low.								  " << endl << endl << endl << endl<< endl;
}
}

Pause();

return EXIT_SUCCESS;
}

```

• VII. SET EXAMPLE:

The syntax for instantiating a set object is as follows:

set <type, OptionalBinaryPredicate> objectName;

set is a C++ keyword. The angle brackets contain the word type <type, OptionalBinaryPredicate> which is a placeholder for the type of the set you are instantiating. The angle brackets also contain the word OptionalBinaryPredicae which is an optional binary sort predicate. The default sort predicate invokes the less than < operator and sorts in ascending order, low to high. objectName is the name of the set object you are instantiating.

A set iterator is declared using the following syntax:

set<type>::iterator iterator_name;

C++ set objects include a member function called begin(). Using begin() returns an iterator to the beginning, the first element, of the set.

A set iterator is initialized using the following syntax:

iterator_name = set_name.begin();

C++ set objects also include a member function called end(). Be careful when you use end(). Using end() returns an iterator to one past the last element, of a set.

When you examine the code in the attached set example program you will see the data used to initialize the set was shuffled prior to insertion.

During insertion the binary predicate assisted in the decending order sort of the data.

Attached is the set example program:

```//**************************************
//C++ STL BINARY PREDICATE FUNCTIONS
//TUTORIAL
//Set Example:
//***************************************
#ifndef CALC_ERROR_H
#define CALC_ERROR_H
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <set>
#include <sstream>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#endif  //CALC_ERROR_H

//************************************
//Class CC
//************************************
class CC
{
private:

string Number;						   //Temperature equation number.
string Equation;						 //Temperature conversion equation.

public:

CC()									 //Constructor used when neither equation number nor conversion equation are entered.
{
Number = " ";
Equation = " ";
}

CC(string n)							 //Constructor used when only conversion equation number is entered.
{
Number = n;						  //n is the temperature conversion equation number.
Equation = " ";
}

CC(string n, string e)				   //Constructor used when both equation number and conversion equation are entered.
{
Number = n;
Equation = e;						//e is the temperature conversion equation.
}

string m_getNumber();					//Temperature conversion equation number.
string m_getEquation();				  //Temperature conversion equation.
};

//************************************
//Gets the temperature conversion
//equation number.
//************************************
string CC::m_getNumber()
{
return Number;
}

//************************************
//Gets the temperature conversion
//equation.
//************************************
string CC::m_getEquation()
{
//cout << endl << endl;
//cout << "  86  string m_getEquation()." << endl;

return Equation;
}

//************************************
//This function mimics a key value pair
//when the Set is output.<b>
...

```

Is This A Good Question/Topic? 2

Replies To: C++ STL BINARY PREDICATE FUNCTION TUTORIAL

#2 Elcric

• D.I.C Regular

Reputation: 102
• Posts: 453
• Joined: 02-May 09

Posted 21 September 2011 - 03:14 PM

DIC is my backup disk. Where is the rest of the Set Example code?

#3 Ricky65

Reputation: 38
• Posts: 115
• Joined: 03-June 10

Posted 23 September 2011 - 03:42 PM

Just skimmed through it and I have a major problem.
You are wrong in stating that list, map and set are keywords; they are STL containers.

It pains me to see such fundamental errors from a tutorial.

In modern C++ (C++11) lambdas are the best way of applying predicates to STL algorithms. Unlike function pointers and function objects you can write your logic in place and unlike functions pointers the code will be inlined.

Consider this example using std::count_if:

```int x [] = {3, 9, 11, 14, 7};
//return the number of elements in x which are greater than 5 and less than 10
auto test_count = std::count_if(std::begin(x), std::end(x), [] (int a) {return a > 5 && a < 10;});
```

It's just so easy. The STL algorithms become a factor of ten more usable (at least) with lambdas.

This post has been edited by Ricky65: 23 September 2011 - 03:45 PM

#4 Elcric

• D.I.C Regular

Reputation: 102
• Posts: 453
• Joined: 02-May 09

Posted 25 September 2011 - 12:07 AM

Ricky65, on 23 September 2011 - 06:42 PM, said:

Just skimmed through it and I have a major problem.
You are wrong in stating that list, map and set are keywords; they are STL containers.

It pains me to see such fundamental errors from a tutorial.

In modern C++ (C++11) lambdas are the best way of applying predicates to STL algorithms. Unlike function pointers and function objects you can write your logic in place and unlike functions pointers the code will be inlined.

Consider this example using std::count_if:

```int x [] = {3, 9, 11, 14, 7};
//return the number of elements in x which are greater than 5 and less than 10
auto test_count = std::count_if(std::begin(x), std::end(x), [] (int a) {return a > 5 && a < 10;});
```

It's just so easy. The STL algorithms become a factor of ten more usable (at least) with lambdas.

Thank you Ricky65. Good catch! I do not know where I picked up the habit of calling STL containers keywords. I goofed and I am sorry. I will review all of the other papers I have written. Thanks!

This post has been edited by Elcric: 25 September 2011 - 12:09 AM