basic_string<wchar_t>.
Some of basic_string's member functions use an unusual method of specifying positions and ranges. In addition to the conventional method using iterators, many of basic_string's member functions use a single value pos of type size_type to represent a position (in which case the position is begin() + pos, and many of basic_string's member functions use two values, pos and n, to represent a range. In that case pos is the beginning of the range and n is its size. That is, the range is [begin() + pos, begin() + pos + n).
Note that the C++ standard does not specify the complexity of basic_string operations. In this implementation, basic_string has performance characteristics very similar to those of vector: access to a single character is O(1), while copy and concatenation are O(N). By contrast, rope has very different performance characteristics: most rope operations have logarithmic complexity.
Note also that, according to the C++ standard, basic_string has very unusual iterator invalidation semantics. Iterators may be invalidated by swap, reserve, insert , and erase (and by functions that are equivalent to insert and/or erase , such as clear, resize, append , and replace). Additionally, however, the first call to any non-const member function, including the non-const version of begin() or operator[] , may invalidate iterators. (The intent of these iterator invalidation rules is to give implementors greater freedom in implementation techniques.) In this implementation, begin(), end(), rbegin(), rend(), operator[], c_str(), and data() do not invalidate iterators. In this implementation, iterators are only invalidated by member functions that explicitly change the string's contents.
Example int main() {
string s(10u, ' '); // Create a string of ten blanks.
const char* A = 'this is a test';
s += A;
cout << 's = ' << (s + '
');
cout << 'As a null-terminated sequence: ' << s.c_str() << endl;
cout << 'The sixteenth character is ' << s[15] << endl;
reverse(s.begin(), s.end());
s.push_back('
');
cout << s;
}
Definition Defined in the standard header string.
Template parameters | Parameter | Description | Default |
charT | The string's value type: the type of character it contains. | |
traits | The Character Traits type, which encapsulates basic character operations. | char_traits<charT> |
Alloc | The string's allocator, used for internal memory management. | alloc |
Model of Random Access Container, Sequence.
Type requirements In addition to the type requirements imposed by Random Access Container and Sequence:
• charT is a POD ('plain ol' data') type.
• traits is a Character Traits type whose value type is charT
Public base classes None.
Members | Member | Where defined | Description |
value_type | Container | The type of object, CharT, stored in the string. |
pointer | Container | Pointer to CharT. |
reference | Container | Reference to CharT |
const_reference | Container |