Several methods deal with memory—for example, clearing memory contents, resizing a string, or adjusting the capacity of a string. Table F.2 lists some memory-related methods.
There are four methods for accessing individual characters, two of which use the []
operator and two of which use the at()
method:
reference operator[](size_type pos);
const_reference operator[](size_type pos) const;
reference at(size_type n);
const_reference at(size_type n) const;
The first operator[]()
method allows you to access an individual element of a string by using array notation; it can be used to retrieve or alter the value. The second operator[]()
method can be used with const
objects, and it can be used only to retrieve the value:
string word("tack");
cout << word[0]; // display the t
word[3] = 't'; // overwrite the k with a t
const ward("garlic");
cout << ward[2]; // display the r
The at()
methods provide similar access, except that the index is provided in function argument notation:
string word("tack");
cout << word.at(0); // display the t
The difference (besides the syntax difference) is that the at()
methods provide bounds checking and throw an out_of_range
exception if pos >= size()
. Note that pos
is type size_type
, which is unsigned; therefore, a negative value is impossible for pos
. The operator[]()
methods don’t do bounds checking, so the behavior is undefined if pos >= size()
, except that the const
version returns the null character equivalent if pos == size()
.
Thus, you get a choice between safety (using at()
and testing for exceptions) and execution speed (using array notation).
There is also a function that returns a new string that is a substring of the original:
basic_string substr(size_type pos = 0, size_type n = npos) const;
It returns a string that’s a copy of the string starting at position pos
and going for n
characters or to the end of the string, whichever comes first. For example, the following initializes pet
to "donkey"
:
string message("Maybe the donkey will learn to sing.");
string pet(message.substr(10, 6));
C++11 adds these four access methods:
const charT& front() const;
charT& front();
const charT& back() const;
charT& back();
The front()
methods access the first element of a string
, acting like operator[](0)
. The back()
methods access the last element of a string
, acting like operator[](size() - 1)
.
C++11 has five overloaded assignment methods, up two from C++98:
basic_string& operator=(const basic_string& str);
basic_string& operator=(const charT* s);
basic_string& operator=(charT c);
basic_string& operator=(basic_string&& str) noexcept; // C++11
basic_string& operator=(initializer_list<charT>); // C++11
The first assigns one string
object to another, the second assigns a C-style string to a string
object, the third assigns a single character to a string
object, the fourth uses move semantics to assign an rvalue string
object to a string
object, and the fifth allows assignment of an initializer list. Thus, all the following operations are possible:
string name("George Wash");
string pres, veep, source, join, awkward;
pres = name;
veep = "Road Runner";
source = 'X';
join = name + source; // now with move semantics!
awkward = {'C','l','o','u','s','e','a','u'};
The string
class provides six search functions, each with four prototypes. The following sections describe them briefly.
3.133.107.25