Namespace
Varianti

Range-based for loop (dal C++11)

Da cppreference.com.
< cpp‎ | language

 
 
Linguaggio C + +
Temi generali
Original:
General topics
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Controllo del flusso
Original:
Flow control
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Dichiarazioni esecuzione condizionale
Original:
Conditional execution statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Iterazione dichiarazioni
Original:
Iteration statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
for loop
range-for loop (C++11)
Vai dichiarazioni
Original:
Jump statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Funzioni
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
dichiarazione di funzione
lambda funzione dichiarazione
funzione di modello
specificatore inline
eccezioni specifiche (deprecato)
noexcept specificatore (C++11)
Eccezioni
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Spazi dei nomi
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Tipi
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decltype specifier (C++11)
Specifiers
Original:
Specifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
cv specificatori
Durata di stoccaggio specificatori
constexpr specificatore (C++11)
specificatore auto (C++11)
alignas specificatore (C++11)
Inizializzazione
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Letterali
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Espressioni
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
rappresentazioni alternative
Utilities
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Tipi
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typedef declaration
Tipo alias dichiarazione (C++11)
attributi (C++11)
Lancia
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
conversioni implicite
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
Fusione C-stile e funzionale
Occupazione della memoria
Original:
Memory allocation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Classi
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Specifiche per una classe di funzioni proprietà
Original:
Class-specific function properties
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
esplicito (C++11)
statico
Funzioni membro speciali
Original:
Special member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Modelli
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
classe template
funzione di modello
modello di specializzazione
parametri confezioni (C++11)
Varie
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Montaggio in linea
 
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.
tradizionale in un intervallo di valori, ad esempio, da qualche contenitore o lista.
Original:
Used as a more readable equivalent to the traditional
The text has been machine-translated via Google Translate.
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.
{
auto && __range = range_expression ;
auto __begin = begin_expr(__range);
auto __end = end_expr(__range);
for (;__begin != __end; ++__begin) {
range_declaration = *__begin;
loop_statement
}

}

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 è legato
    Original:
    (__range) and (__range + __bound) for array types, where __bound is the array bound
    The 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.
può essere utilizzato per uscire dal ciclo precoce e
Istruzione continue
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.
può essere utilizzato per riavviare il ciclo con il successivo elemento.

[modifica] Parole chiave

for

[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