Bibliothèque de chaînes C ++ - opérateur []

La description

Il renvoie une référence au caractère à la position pos dans la chaîne.

Déclaration

Voici la déclaration pour std :: string :: operator []

char& operator[] (size_t pos);

C ++ 11

const char& operator[] (size_t pos) const;

Paramètres

pos - Valeur avec la position d'un caractère dans la chaîne.

Valeur de retour

Il renvoie une référence au caractère à la position pos dans la chaîne.

Des exceptions

si une exception est levée, il n'y a aucun changement dans la chaîne.

Exemple

Dans l'exemple ci-dessous pour std :: string :: operator [].

#include <iostream>
#include <string>

int main () {
   std::string str ("Sairamkrishna Mammahe");
   for (int i=0; i<str.length(); ++i) {
      std::cout << str[i];
   }
   return 0;
}
Sairamkrishna Mammahe