Aracılığıyla paylaş


basic_string::at

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

const_reference at(
    size_type _Off
) const;
reference at(
    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.

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

Üye operator[] bir parametre olarak geçirilen dizin geçerli olup olmadığı, ancak üye işlev denetlemez en desteklemez ve bu nedenle kullanılmalıdır geçerlilik belli değilse.Sıfır bir dizin daha az olduğu bir geçersiz dizin üye işlevine geçirilen dize boyutuna eşit veya ondan büyük en atan bir out_of_range sınıfı özel durum.Geçersiz bir dizin geçirilen operator[] sonuçlarında undefined davranışı, ancak dizin dize uzunluğunu eşit const dizeleri için geçerli bir dizin ve operatör null-bu dizin geçirildiğinde karakteri verir.

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

Örnek

// basic_string_at.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 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 8 in the const string cstr2 is: n.

Gereksinimler

Başlık: <string>

Namespace: std

Ayrıca bkz.

Başvuru

basic_string Class