#include <string>
using std::string;
void main() {
string s1 = "Hello";
char ch = s1[4]; // uses const version
s1[0] = 'J'; // uses non-const version
char ch2 = s1[5]; // Bad error - unchecked access!
// For checked access, use corresponding
// function named at( pos ):
char ch3 = s1.at( 5 ); // Bad error - unchecked access!
}
The s1.at(pos) behaves just as s1[pos], except
that it also performs a range check, throwing an exception of type out_of_range
in case that pos is not an actual position in the string.