Abstract
If we use the C statement in combination with the circle, this is a very simple topic, but if we use C ++ with STL, there will be a very beautiful method.
Introduction
The values column has five values: 2, 3, 1, 3, and 5. You want to list all elements larger than 2.
C ++
1 /*
2 (C) oomusou 2008 Http://oomusou.cnblogs.com
3
4 Filename: genericalgo_remove_copy_if_predicate.cpp
5 Compiler: Visual C + + 8.0
6 Description: Demo how to use remove_copy_if () & Predicate
7 Release: 01/29/2008 1.0
8 */
9
10 # Include < Iostream >
11 # Include < Algorithm >
12 # Include < Functional >
13
14 Using Namespace STD;
15
16 Int Main (){
17 Int IA [] = { 2 , 3 , 1 , 3 , 5 };
18 Int Arr_size = Sizeof (Ia) / Sizeof ( Int );
19
20 Remove_copy_if (IA, IA + Arr_size, ostream_iterator < Int > (Cout, " " ), Bind2nd (less_equal < Int > (), 2 ));
21 }
Results
3 3 5
20 rows
Remove_copy_if (IA, IA + Arr_size, ostream_iterator < Int > (Cout, " " ), Bind2nd (less_equal < Int > (), 2 ));
We know that copy () can display the elements in the response column through ostream_interator. Currently, we only want to list elements larger than 2. We will directly look for copy_if (), however, STL does not provide copy_if (), but only remove_copy_if (), so you have to think backwards, the resulting "greater than 2" means removing all "less than 2" elements, so we must use less_equal <int> () This binary predicate, because it is now "less than 2", you also need bind2nd () to give 2 bind to less_equal <int> ().
Conclusion
This method of functional programming is not the same as the traditional method. It takes some time to adapt, C # Father Anders hejisberg also believes that functional programming will become more and more important, so C #3 is also beginning to support lambda, STL and boost have already pushed down the functional programming.