Range-based for loop (seit C++11)
Aus cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
<metanoindex/>
Führt eine for-Schleife über einen Bereich .
Original:
Executes a for loop over a range.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Wird als lesbarer äquivalent zu der traditionellen
for-Schleife
Betriebs über einen Bereich von Werten, zum Beispiel von einigen Behälter oder Liste .Original:
for loop
The text has been machine-translated via [http://translate.google.com Google Translate].
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
Original:
Used as a more readable equivalent to the traditional
for-Schleife
operating over a range of values, for example, from some container or list.Original:
for loop
The text has been machine-translated via [http://translate.google.com Google Translate].
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Syntax
for ( range_declaration : range_expression) loop_statement
|
|||||||||
Erklärung
Die obige Syntax erzeugt Code ähnlich dem folgenden (
__range, __begin und __end sind für die Ausstellung only):Original:
The above syntax produces code similar to the following (
__range, __begin and __end are for exposition only):The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
{
|
|||||||||
Die range_expression wird ausgewertet, um die Reihenfolge zu bestimmen oder den Bereich übernimmt iteriert werden. Jedes Element der Sequenz dereferenziert und übertragen auf die Variable mit dem Typ und den Namen in der range_declaration gegeben .
Original:
The range_expression is evaluated to determine the sequence or range will be iterated over. Each element of the sequence is dereferenced, and assigned to the variable using the type and name given in the range_declaration.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Die
begin_expr und end_expr werden definiert, um entweder:Original:
The
begin_expr and end_expr are defined to be either:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
(__range)und(__range + __bound)für Array-Typen, wo__boundist das Array gebundenOriginal:(__range)and(__range + __bound)for array types, where__boundis the array boundThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.begin(__range)undend(__range), die basierend auf Argument-Lookup Regeln gefunden. Für Standard-Containern endet es gleichwertig std::begin und std::end was wiederum Anrufe__range.begin()und__range.end().Original:begin(__range)andend(__range), which are found based on argument-lookup rules. For standard containers this ends up being equivalent to std::begin and std::end which in turn calls__range.begin()and__range.end().The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Wenn eine vorübergehende range_expression zurückkehrt, wird die Lebensdauer bis zum Ende der Schleife verlängert, wie durch die Bindung an den R-Wert-Referenz
__range angegeben .Original:
If range_expression returns a temporary, its lifetime is extended until the end of the loop, as indicated by binding to the rvalue reference
__range.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Genau wie bei einem traditionellen Schleife können
break-Anweisung
verwendet werden, um die Schleife frühzeitig verlassen und Original:
break statement
The text has been machine-translated via [http://translate.google.com Google Translate].
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
continue-Anweisung
können verwendet werden, um die Schleife mit dem nächsten Element neu zu starten .Original:
continue statement
The text has been machine-translated via [http://translate.google.com Google Translate].
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
Original:
Just as with a traditional loop,
break-Anweisung
can be used to exit the loop early and Original:
break statement
The text has been machine-translated via [http://translate.google.com Google Translate].
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
continue-Anweisung
can be used to restart the loop with the next element.Original:
continue statement
The text has been machine-translated via [http://translate.google.com Google Translate].
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Keywords
Beispiel
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v = {0, 1, 2, 3, 4, 5};
for (int &i : v) // access by reference (const allowed)
std::cout << i << ' ';
std::cout << '\n';
for (auto i : v) // compiler uses type inference to determine the right type
std::cout << i << ' ';
std::cout << '\n';
for (int i : v) // access by value as well
std::cout << i << ' ';
std::cout << '\n';
}
Output:
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5