Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Clear11 range for statement

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report