C++ String Library - at


Advertisements

Description

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

Declaration

Following is the declaration for std::string::at.

char& at (size_t pos);

C++11

const char& at (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::at.

#include <iostream>
#include <string>

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