C++ String Library - operator[]


Advertisements


Description

It returns a reference to the character at position pos in the string.

Declaration

Following is the declaration for std::string::operator[]

 char& operator[] (size_t pos);

C++11

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

Parameters

pos − Value with the position of a character within the string.

Return Value

It returns a reference to the character at position pos in the string.

Exceptions

if an exception is thrown, there are no changes in the string.

Example

In below example for std::string::operator[].

Live Demo
#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 

string.htm

Advertisements