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. |
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.
You can help to correct and verify the translation. Click here for instructions.
Utilizzato come equivalente più leggibile al funzionamento <div class="t-tr-text"> ciclo for
tradizionale in un intervallo di valori, ad esempio, da qualche contenitore o lista.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
ciclo for</div> 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.
Indice |
[modifica] Sintassi
for ( range_declaration : range_expression) loop_statement
|
|||||||||
[modifica] 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.
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.
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.
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__bound
is 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) e end(__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) and end(__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.
You can help to correct and verify the translation. Click here for instructions.
Proprio come con un ciclo tradizionale, <div class="t-tr-text"> Istruzione break
può essere utilizzato per uscire dal ciclo precoce e 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.
Istruzione continue
può essere utilizzato per riavviare il ciclo con il successivo elemento.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,
Istruzione 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.
Istruzione continue</div> 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.
[modifica] Parole chiave
[modifica] 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