Range-based for loop (dal C++11)
Da cppreference.com.
|
|
Questa pagina è stata tradotta in modo automatico dalla versione in ineglese della wiki usando Google Translate. La traduzione potrebbe contenere errori e termini strani. Muovi il puntatore sopra al testo per vedere la versione originale. Puoi aiutarci a correggere gli gli errori. Per ulteriori istruzioni clicca qui. |
<metanoindex/>
Esegue un ciclo for in un campo.
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.
Sintassi
for ( range_declaration : range_expression) loop_statement
|
|||||||||
Spiegazione
La sintassi di cui sopra produce codice simile al seguente (__range, __begin e __end sono solo per l'esposizione):
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.
{
|
|||||||||
Il range_expression viene valutata per determinare la sequenza o l'intervallo verrà iterata. Ogni elemento della sequenza è dereferenziato, e assegnato alla variabile con il tipo e il nome dato nel 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.
Il begin_expr e end_expr sono definiti devono riguardare:
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.
(__range)(__range + __bound)e per i tipi di matrice, in cui la matrice__boundè legatoOriginal:
(__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)eend(__range), che si trovano sulla base di argomenti di ricerca-regole. Per i contenitori standard, questo finisce per essere equivalente a std::begin std::end e che richiede a sua volta__range.begin()e__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.
Se range_expression restituisce un temporaneo, la sua durata è estesa fino alla fine del ciclo, come indicato legandosi al riferimento 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.
Parole chiave
Esempio
#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