basic_string Class

The sequences controlled by an object of template class basic_string are the Standard C++ string class and are usually referred to as strings, but they should not be confused with the null-terminated C-strings used throughout the Standard C++ Library. The string class is a container that enables the use of strings as normal types, such as using comparison and concatenation operations, iterators, and STL algorithms and copying and assigning with class allocator managed memory.

For a list of all members of this type, see basic_string Members.

template <
   class CharType,
   class Traits=char_traits<CharType>, 
   class Allocator=allocator<CharType> 
class basic_string


  • CharType
    The data type of a single character to be stored in the string. The Standard C++ Library provides two specializations of this template class, with the type definitions string, for elements of type char, and wstring, for elements of type wchar_t.

  • Traits
    Various important properties of the CharType elements in a basic_string specialization are described by the class Traits.

  • Allocator
    The type that represents the stored allocator object that encapsulates details about the string's allocation and deallocation of memory. The default value is allocator*<Type>.*

Many member functions require an operand sequence of elements. You can specify such an operand sequence several ways.




One element with character value _Ch.

_Count, _Ch

A repetition of _Count elements each with value _Ch.


A null-terminated sequence, such as a C string, with a CharType of type char, beginning at _Ptr (which must not be a null pointer), where the terminating element is the value value_type and is not part of the operand sequence.

_Ptr, _Count

A sequence of _Count elements beginning at _Ptr.


The sequence specified by a basic_string object.

_Str, _Off, _Count

The substring of the basic_string object _Str with up to _Count elements (or through the end of the string, whichever comes first) beginning at position _Off.

_First, _Last

A sequence of elements delimited by the iterators _First and _Last, in the range [_First, _Last), which may overlap the sequence controlled by the string object whose member function is being called.

If a position argument (such as _Off) is beyond the end of the string on a call to a basic_string member function, the function reports an out-of-range error by throwing an object of type out_of_range Class.

If a function is asked to generate a sequence longer than max_size elements, the function reports a length error by throwing an object of type length_error Class.

References, pointers, and iterators that designate elements of the controlled sequence can become invalid after any call to a function that alters the controlled sequence, or after the first call to a non-const member function.


Header: <string>

Namespace: std

See Also


Thread Safety in the Standard C++ Library

Other Resources

basic_string Members

<string> Members