How do I list all the elements of N in the limit column? (C/C ++) (STL)

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.