Range-based for loop (depuis C++11)
De 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/>
Exécute une boucle for sur une plage .
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.
Utilisé comme un équivalent plus lisible pour l'exploitation traditionnelle
pour la boucle
sur une plage de valeurs, par exemple, de certains conteneurs ou 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
pour la boucle
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.
Syntaxe
for ( range_declaration : range_expression) loop_statement
|
|||||||||
Explication
La syntaxe ci-dessus produit un code semblable au suivant (
__range, __begin et __end sont pour l'exposition seulement):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.
{
|
|||||||||
La range_expression est évalué pour déterminer la séquence ou la plage sera itéré. Chaque élément de la séquence est déréférencé, et affecté à la variable en utilisant le type et le nom donné dans la range_declaration .
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.
La
begin_expr et end_expr sont définis pour être soit: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)et(__range + __bound)pour les types tableau, où__boundest le tableau liéOriginal:(__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)etend(__range), ce qui peut se fonder sur l'argument-lookup règles. Pour les conteneurs standards cela finit par être équivalent à std::begin et std::end qui appelle à son tour__range.begin()et__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.
Si range_expression retourne un temporaire, sa durée de vie est prolongée jusqu'à la fin de la boucle, comme indiqué par la liaison à la référence rvalue
__range .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.
Tout comme avec une boucle traditionnelle,
instruction break
peut être utilisé pour sortir de la boucle précoce et 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.
instruction continue
peut être utilisé pour redémarrer la boucle avec l'élément suivant .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,
instruction break
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.
instruction continue
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.
Mots-clés
Exemple
#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';
}
Résultat :
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5