Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the Clear11 range for statement

Shulou Source: shulou.com Published: 2022-06-01 19:48:15 12月04日 Update

This article mainly explains "how to use for sentences in the scope of Category 11". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. now please follow the editor's train of thought to study and learn how to use the for sentences in the scope of Category 11.

The new style of C++

Ten years ago, C++ language and programmers would have been quite confident. But in recent years, a variety of new languages, new architectures continue to emerge, although the status of C++ is not wavering, but it is true that the limelight has been stolen on many occasions. So C++ 's code style is also changing. Today's scope for statement is an example.

Function introduction

The most common scenario of a loop is to perform some operation on each element of the data collection. Before Category 11, the program looked something like this:

Int a [] = {1,2,3,4}

Int sum = 0

For (int I = 0; I < sizeof (a) / sizeof (a [0]); + + I) {

Sum + = a [I]

}

Or like this.

Vector v {1, 2, 3,4}

Int sum = 0

For (auto it = v.begin (); it! = v.end (); it++) {

Sum + = * it

}

As you can see, although the goal is to sum each element in the data set, there are a few things to do:

Define and initialize loop variables

Judge cycle condition

Adjust cyclic variable

Although C++ programmers are familiar with these processes, they are still in some trouble. Probably for this reason, range for statement was introduced in Clover 11:

Nt array [] {1, 2, 3, 4}

Int sum = 0

For (int a: array) {

Sum + = a

}

Vector vect {1, 2, 3, 4}

Int sum = 0

For (int v: vect) {

Sum + = v

}

For (int v: vect) is read as "for every v in vect". It should be said that the program is quite concise.

Application condition

Is it possible that all data sets can be handed over to the scope for traversal? The answer is no.

The condition that data v is traversed by range for is that it supports v.begin () / v.end () or begin (v) / end (v) and returns an iterator. All the containers in STL meet the above conditions. The C++ compiler provides a mechanism equivalent to the above interface for arrays of built-in types, so it can also be used in the scope for.

Thank you for your reading, the above is the content of "how to use for sentences in the scope of Category 11". After the study of this article, I believe you have a deeper understanding of how to use the for sentences in the scope of Category 11, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Scope statements clocking data conditions programs loops learning elements content variables that is programmers or style concise in recent years constantly facts examples Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Information MariaDB Apple MySQL