Implement a foreach loop in C ++

Source: Internet
Author: User
Friends who have used more advanced languages such as C # and PHP must know the foreach loop. The foreach statement brings a great deal to programming.
This reduces the code and increases the readability of the program. C ++ does not support the foreach keyword,
However, C ++ is a powerful language. You can also use the foreach loop in C ++ with a little tips. In C #, the effect of the foreach statement is surprising. The foreach statement saves complex and similar loop control statements, making the code very concise and clear. The following example shows this point clearly. Assume that there is a container of the containertype type, and its element type is elementtype. In C #, arrays are such a container. In C ++, STL vector and map are also such containers. To traverse every element of the Container, use the following code in C #: foreach
(Elementtype Element
In
Container ){
//
Use element to access this element}. In C ++, the Common Code is as follows: container: iterator ITER;
(ITER
=
Container. Begin (); ITER
! =
Container. End (); ITER
++
){
//
Use (* ITER) to access this element}
Obviously, the code in C # is much clearer. The C ++ Code contains variable declaration and loop control, which is more complex. You may not care about the increasing complexity of this point, but in the spirit of the principle of not doing anything because of good or small, and a bit of thinking, I decided to enable C ++ TO USE THE foreach loop. The macros of C ++ are very suitable for such tasks. The problem is how to make it more like the style in C. If you add a macro to the front and back of the code block that accesses the element, it becomes meaningless and ridiculous. We can only use one macro and name it foreach. In this
The code block after the macro can use a variable name as in C # to traverse all elements in the container. The variables declared in the macro cannot be contaminated with the names of other parts of the program, and these variables are not accessed.
It is declared in the code block of the element. This requirement turns this problem into an interesting question that tests code skills. The powerful control capability of C ++ enables it to support foreach statements. This requires two tips: 1. the first statement of the for loop can declare local variables; 2. Make full use of the process control capability of the for loop. The foreach statement Definition macro in C ++ is as follows:

# Define foreach (elementtype, element, containertype, container )/

For
(Containertype: iterator ITER
=
Container.
Begin
(); ITER
! =
Container.
End
(); ITER
++
)/

For
(Bool
Go
 
=
True;
Go
;)/

For
(Elementtype
&
Element
=
 
*
ITER;
Go
;
Go
=
False)

Macro defines a triple for loop. The first significance is clear: Use the traversal of the container to traverse elements in the container. Second, the third for loop can only be executed once. Its main purpose is to declare the element variable. The variable reference type of C ++ enables element variables to be directly used for element access code blocks. Foreach
Macro parameters are in sequence: Element type, element name, container type, and container name. Assume that a vector is defined as follows: vector <int
> V; then, you can use the following code to traverse V: foreach (int
, E, vector <int
>, V ){//
Using (e) to access V elements} is it like the foreach statement in C? I wrote a short program to see if the macro really works. The Code is as follows: # include
<Iostream>
# Include
<Vector>
Using
Namespace
STD;
//
Use a foreach loop in C ++
# Define
Foreach (elementtype, element, containertype, container )/
For
(Containertype: iterator iter = container. Begin (); iter! = Container. End (); ITER ++ )/
For
(Bool
Go = true
; Go ;)/
For
(Elementtype & element = * ITER; go = false
)
Int
Main ()
{
Vector <int
> V;
For
(Int
I = 0; I <10; ++ I)
V. push_back (I );
Foreach (int
, E, vector <int
>, V )//
Use foreach here
Statement


{
Printf ("% d/N", e );
}
}
The result output is correct! In fact, in C ++, for statements and other statements are essentially process control statements based on computer commands, while foreach statements in C, it can be seen as a statement in the Design Pattern of the baseline traversal. The development of programming languages, from computer-based instruction to design-based language, should be regarded as the progress of software development. Using the foreach Statement defined in this article in C ++ does not improve the program efficiency or significantly reduce the efficiency. Foreach macros can only have some advantages at the code level, reduce some repetitive code and increase readability. Designing this macro is just a matter of fun, fun, and support for those who firmly embrace C ++. Author: Su Lin

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.