Description

The template class describes an object that controls a varying-length sequence of elements of type E, also known as value_type. Such an element type must not require explicit construction or destruction, and it must be suitable for use as the E parameter to basic_istream or basic_ostream (A "plain old data structure", or POD, from C generally meets this criterion.) 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.

Various important properties of the elements in a basic_string specialization are described by the class T, also known as traits_type. A class that specifies these character traits must have the same external interface as an object of template class char_traits.

The object allocates and frees storage for the sequence it controls through a stored allocator object of class A, also known as allocator_type. Such an allocator object must have the same external interface as an object of template class allocator. (Class char_traits has no provision for alternate addressing schemes, such as might be required to implement a far heap.) Note that the stored allocator object is not copied when the container object is assigned.

The sequences controlled by an object of template class basic_string are usually called strings. These objects should not be confused, however, with the null-terminated C strings used throughout the Standard C++ Library.

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

If a position argument (such as pos above) 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 class out_of_range.

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 class length_error.

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 the non-const member functions at, begin, end , operator[], rbegin, or rend. (The idea is to permit multiple strings to share the same representation until one string becomes a candidate for change, at which point that string makes a private copy of the representation, using a discipline called copy on write.)