Aracılığıyla paylaş


basic_string::operator[]

Belirtilen bir dize dizini karakteri için bir başvuru sağlar.

const_reference operator[](
   size_type _Off
) const;
reference operator[](
   size_type _Off
);

Parametreler

  • _Off
    Başvurulacak öğenin konumunu dizin.

Dönüş Değeri

Karakter dizesi parametresi Index ile belirtilen konumdaki bir başvuru.

Notlar

Dizenin ilk öğe sıfır dizini vardır ve aşağıdaki öğeleri sırayla pozitif tamsayılar dizine böylece uzunlukta bir dize n olan bir nth öğe dizine sayısı ile n - 1.

**operator[]**üye işlevi hızlıdır en okuma ve yazma erişimi için bir dize öğelerinin sağlamak için.

**operator[]**bir parametre olarak geçirilen dizin geçerli olup olmadığı, ancak üye işlev denetlemez en yaptığı ve geçerlilik kullanılması gereken şekilde kesin değildir. Geçersiz bir dizin (sıfır bir dizin daha az dize boyutuna eşit veya ondan büyük) üye işlevine geçirilen en atan bir out_of_range sınıfı özel durum. Geçersiz bir dizin geçirilen operator[] sonuçları undefined davranışı, ancak dizin dize uzunluğunu eşit const dizeleri için geçerli bir dizin ve operatörü bu dizin geçirildiğinde null karakteri döndürür.

Başvuru döndürülen geçersiz dize adetle sınırla ya da değişiklik olmayan-const dizeleri.

1 _secure_scl ile derlerken dize sınırları dışında bir öğe erişmeye çalışırsanız, bir çalışma zamanı hatası ortaya çıkar. Daha fazla bilgi için bkz. İşaretli Yineleyiciler.

Örnek

// basic_string_op_ref.cpp
// compile with: /EHsc
#include <string>
#include <iostream>

int main( )
{
   using namespace std;
   string str1 ( "Hello world" ), str2 ( "Goodbye world" );
   const string cstr1 ( "Hello there" ), cstr2 ( "Goodbye now" );
   cout << "The original string str1 is: " << str1 << endl;
   cout << "The original string str2 is: " << str2 << endl;

   // Element access to the non-const strings
   basic_string <char>::reference refStr1 = str1 [6];
   basic_string <char>::reference refStr2 = str2.at ( 3 );

   cout << "The character with an index of 6 in string str1 is: "
        << refStr1 << "." << endl;
   cout << "The character with an index of 3 in string str2 is: "
        << refStr2 << "." << endl;

   // Element access to the const strings
   basic_string <char>::const_reference crefStr1 = cstr1 [ cstr1.length ( ) ];
   basic_string <char>::const_reference crefStr2 = cstr2.at ( 8 );

   if ( crefStr1 == '\0' )
      cout << "The null character is returned as a valid reference."
           << endl;
   else
      cout << "The null character is not returned." << endl;
   cout << "The character with index of 8 in the const string cstr2 is: "
        << crefStr2 << "." << endl;
}

Çıktı

The original string str1 is: Hello world
The original string str2 is: Goodbye world
The character with an index of 6 in string str1 is: w.
The character with an index of 3 in string str2 is: d.
The null character is returned as a valid reference.
The character with index of 8 in the const string cstr2 is: n.

Gereksinimler

Başlık: <string>

Namespace: std

Ayrıca bkz.

Başvuru

basic_string Class