![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
To test the member function, you can execute the following program: // This is the program to test // persname class // pn1test.cpp #include <iostream> #include persnam1.h int main() { persname Names; Names.Add (Andrew); Names.Add (Peter); Names.Add (John); Names.Add (Andrew); Names.Add (Andrew); Names.Add (Margaret); Names.Sort(); Names.DisplayAll(); cout << endl; // The following lines are new; they show the usage of FindNameJ cout << The first name starting with J is << Names.FindNameJ() << endl; return 0; } The program should return Andrew Andrew Andrew Andrew John Margaret Peter The first name starting with J is John Comments To increase the functionality of certain algorithms, you can use predicates to specify the criteria that the algorithm uses when processing data. A few algorithms change their names by adding the _if suffix. Other algorithms are overloaded and use the same names. Predicates can be unary or binary depending on the number of arguments. A predicate is a function that results in truth or falsehood depending on certain conditions. Functions that change their names with _if, such as count_if and find_if, accept unary predicates. Functions that use overloaded names, such as unique, accept binary predicates. The use of predicates dramatically increases the power of algorithms. It almost doubles their number and provides a programmer with the ability to embed his own functions in the standard algorithms. 9.3 Repeat an action with all elements in a container range?Problem I want to be able to apply a certain function to an entire container or to the range of a container. For example, I often have to convert an area from square feet to square meters and print the results. Do I have to use for loops or is there another way of coding? Technique The Standard Template Library has an analog of the C++ for statement. This is the for_each algorithm that can be applied to any sequential container. Moreover, the algorithm can work on any data structure that has a sequential order similar to an array. The for_each algorithm applies the specified function in forward order, from the beginning to the end of the range. In this How-To, you will write a few examples to show the for_each algorithm functionality. Steps
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|