cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : Strings library : string : at
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
Strings library
char_traits
classes:
· string
global functions:
· getline
· operator+
· operator<<
· operator>>
· comparison operators
· swap
string
string::string
member constants:
· string::npos
member functions:
· string::append
· string::assign
· string::at
· string::begin
· string::capacity
· string::clear
· string::compare
· string::copy
· string::c_str
· string::data
· string::empty
· string::end
· string::erase
· string::find
· string::find_first_not_of
· string::find_first_of
· string::find_last_not_of
· string::find_last_of
· string::get_allocator
· string::insert
· string::length
· string::max_size
· string::operator+=
· string::operator=
· string::operator[]
· string::push_back
· string::rbegin
· string::rend
· string::replace
· string::reserve
· string::resize
· string::rfind
· string::size
· string::substr
· string::swap

-

string::at public member function
const char& at ( size_t pos ) const;
      char& at ( size_t pos );

Get character in string

Returns the character at position pos in the string.

This member function behaves as operator[] , except that at 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.

Parameters

pos
Position within the string of the character to be retrieved. Notice that the first character in the string has position 0.
If the position passed is past the end of str, an out_of_range exception is thrown.
size_t is an unsigned integral type.

Return value

The character at the specified position in the string.

Example

// string::at
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str ("Test string");
  int i;
  for (i=0; i < str.length(); i++)
  {
    cout << str.at(i);
  }
  return 0;
}

This code prints out the content of a string character by character using the at member function.

Basic template member declaration

( basic_string<charT,traits,Allocator> )
typedef typename Allocator::const_reference const_reference;
typedef typename Allocator::reference       reference;
typedef typename Allocator::size_type       size_type;
const_reference at ( size_type pos ) const;
reference       at ( size_type pos );

See also

string::operator[] Get character in string (public member function)
string::substr Generate substring (public member function)
string::find Find content in string (public member function)
string::replace Replace part of string (public member function)

Home page | Privacy policy
© cplusplus.com, 2000-2008 - All rights reserved - v2.2
Spotted an error? contact us