Udostępnij za pośrednictwem


Range-based for — instrukcja (C++)

Wykonuje statement wielokrotnie i kolejno dla każdego elementu w expression.

for ( for-range-declaration : expression )
   statement 

Uwagi

Użyj oparte na zakresie for instrukcji do konstruowania pętli, które należy wykonać poprzez "różne", która jest zdefiniowana jako wszystko, co można wykonać iterację — na przykład, std::vector, lub wszelkie inne sekwencje STL, której zakres jest określony przez begin() i end().Nazwa, która jest zadeklarowana w for-range-declaration część jest kontem lokalnym for instrukcji i nie może być re-declared w expression lub statement.Należy zauważyć, że auto słowo kluczowe jest preferowany w for-range-declaration część instrukcji.

Ten kod pokazuje sposób używania wahał się for pętli Iterować tablicę i wektora:

// range-based-for.cpp
// compile by using: cl /EHsc /nologo /W4
#include <iostream>
#include <vector>
using namespace std;

int main() 
{
    // Basic 10-element integer array.
    int x[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

    // Range-based for loop to iterate through the array.
    for( int y : x ) { // Access by value using a copy declared as a specific type. 
                       // Not preferred.
        cout << y << " ";
    }
    cout << endl;

    // The auto keyword causes type inference to be used. Preferred.

    for( auto y : x ) { // Copy of 'x', almost always undesirable
        cout << y << " ";
    }
    cout << endl;

    for( auto &y : x ) { // Type inference by reference.
        // Observes and/or modifies in-place. Preferred when modify is needed.
        cout << y << " ";
    }
    cout << endl;

    for( const auto &y : x ) { // Type inference by reference.
        // Observes in-place. Preferred when no modify is needed.
        cout << y << " ";
    }
    cout << endl;
    cout << "end of integer array test" << endl;
    cout << endl;

    // Create a vector object that contains 10 elements.
    vector<double> v;
    for (int i = 0; i < 10; ++i) {
        v.push_back(i + 0.14159);
    }

    // Range-based for loop to iterate through the vector, observing in-place.
    for( const auto &j : v ) {
        cout << j << " ";
    }
    cout << endl;
    cout << "end of vector test" << endl;
}

Poniżej przedstawiono dane wyjściowe:

1 2 3 4 5 6 7 8 9 10

1 2 3 4 5 6 7 8 9 10

1 2 3 4 5 6 7 8 9 10

1 2 3 4 5 6 7 8 9 10

end of integer array test


0.14159 1.14159 2.14159 3.14159 4.14159 5.14159 6.14159 7.14159 8.14159 9.14159

end of vector test

Oparte na zakresie for pętla kończy działanie, gdy jeden z tych w statement jest wykonywany: podziału, powrócić, lub Przejdź do na instrukcję oznaczonych poza oparte na zakresie dla pętli.A kontynuować instrukcji w oparte na zakresie for pętla kończy działanie tylko bieżącej iteracji.

Należy pamiętać te fakty dotyczące oparte na zakresie for:

  • Automatycznie rozpoznaje tablic.

  • Rozpoznaje pojemniki, które .begin() i .end().

  • Używa odnośników zależnych od argumentu begin() i end() dla żadnych innych czynności.

Zobacz też

Informacje

Auto słowa kluczowego (Typ potrącenia)

Instrukcje iteracji (C++)

Słów kluczowych języka C++

podczas gdy instrukcja (C++)

do-while — instrukcja (C++)

dla instrukcji (C++)