Range-based for loop (desde C++11)
Da 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. |
Executa um loop em uma faixa.
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.
Utilizado como um equivalente de mais legível para o funcionamento <div class="t-tr-text"> loop
tradicional ao longo de um intervalo de valores, por exemplo, a partir de algum recipiente ou 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
loop</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.
Índice |
[editar] Sintaxe
for ( range_declaration : range_expression) loop_statement
|
|||||||||
[editar] Explicação
A sintaxe acima produz um código semelhante ao seguinte (
__range
, __begin
e __end
são para exposição apenas):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.
{
|
|||||||||
O range_expression é avaliada para determinar a sequência ou a gama será iterado. Cada elemento da seqüência é dereferenced, e atribuído à variável usando o tipo e nome no 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.
O
begin_expr
e end_expr
são definidos para ser: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) e (__range + __bound) para tipos de matriz, onde
__bound
é a matriz vinculadoOriginal:(__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), que são encontrados com base no argumento de pesquisa-regras. Para contêineres padrão isso acaba sendo equivalente a std::begin e std::end que por sua vez exige __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 retorna uma temporária, o seu tempo de vida é prolongado até ao final do ciclo, tal como indicado através da ligação com a referência 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.
Da mesma forma que com um ciclo tradicional, <div class="t-tr-text"> quebrar comunicado
pode ser utilizada para sair do ciclo inicial 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.
continuar comunicado
pode ser utilizado para reiniciar o ciclo com o elemento seguinte.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,
quebrar comunicado
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.
continuar comunicado</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.
[editar] Palavras-chave
[editar] Exemplo
#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'; }
Saída:
0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5