Aracılığıyla paylaş


Range-based for Deyimi (C++)

Yürütür statement tekrar tekrar ve sırayla her öğe için expression.

for ( for-range-declaration : expression )
   statement 

Notlar

Aralık tabanlı kullanmak for "arasında yineleme herhangi bir şey olarak tanımlanan bir aralığı" aracılığıyla yürütmelisiniz döngüleri oluşturmak için deyim — Örneğin, std::vector, veya bağlantı aralığı tarafından tanımlanan diğer stl sıralı bir begin() ve end().İçinde bildirilen ad for-range-declaration bölümü için yerel olan for deyimi içinde re-declared olamaz expression veya statement.Dikkat edin Otomatik anahtar sözcüğünü de tercih edilen for-range-declaration deyiminin bir bölümü.

Bu kodu nasıl kullanılacağını gösterir aralık oluşturulmuş for döngüler bir array ve vector yinelemek için:

// 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;
}

Çıktı şöyledir:

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

Aralık tabanlı for döngüsü sonlanýr bunlardan biri ile statement yürütülür: bir sonu, dönmek, veya goto etiketli deyimi dışında aralık tabanlı için döngü.A devam aralık esaslı deyiminde for yalnızca geçerli yineleme döngüsü sonlanır.

Bu olgular hakkında aralık esaslı aklınızda for:

  • Diziler otomatik olarak tanır.

  • Sahip kapsayıcı tanıdığı .begin() ve .end().

  • Bağımsız değişken bağımlı arama kullanır begin() ve end() başka bir şey için.

Ayrıca bkz.

Başvuru

Otomatik anahtar sözcüğü (Kesintinin türü)

Yineleme ifadeleri (C++)

C++ anahtar sözcükler

İfadesinin (C++)

do-while Deyimi (C++)

Deyimi (C++)