Range-based for loop (начиная с C++11)
Материал из 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. |
Выполняет цикл в диапазоне.
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.
Используется в качестве более читабельным эквивалентна традиционной операционной <div class="t-tr-text"> цикл
в диапазоне значений, например, из некоторых контейнеров или списка.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
цикл</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.
Содержание |
[править] Синтаксис
for ( range_declaration : range_expression) loop_statement
|
|||||||||
[править] Объяснение
Приведенный выше синтаксис производит код, похожий на следующий (
__range, __begin и __end предназначены для экспозиции только)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.
{
|
|||||||||
range_expression оцениваются, чтобы определить последовательность или диапазон будет перемещаться. Каждый элемент последовательности разыменовывается, и присваивается переменной, используя тип и название, данное в 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.
begin_expr и end_expr определены как либо: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) для типов массивов, где
__boundэто массив связаны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) и end(__range), которые находятся на основе аргументов поиска правил. Для стандартных контейнеров этом заканчивает тем, что эквивалентно std::begin и std::end, который в свою очередь, требует __range.begin() и __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.
Если range_expression возвращает временный, срок службы продлен до конца цикла, как указано путем связывания с ссылкой 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.
Так же, как с традиционного цикла, <div class="t-tr-text"> сломать заявлении
может быть использована для выхода из цикла ранних и 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.
продолжить выступление
может быть использована для перезапуска цикла к следующему элементу.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,
сломать заявлении
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.
продолжить выступление</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.
[править] Ключевые слова
[править] Пример
#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'; }
Вывод:
0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5