Recall that the term Allocator is the template parameter name for an allocator class to manage memory. The term Allocator() is the default constructor for that class. Thus, the constructors, by default, use the default version of the allocator object, but they give you the option of using some other version of the allocator object. The following sections examine the constructors individually.
Default Constructor
This is the prototype for the default constructor:
explicit basic_string(const Allocator& a = Allocator());
Typically, you would accept the default argument for the allocator class and would use the constructor to create empty strings:
string bean;
wstring theory;
The following relationships hold after the default constructor is called:
• The data() method returns a non-null pointer to which 0 can be added.
• The size() method returns 0.
• The return value for capacity() is not specified.
Suppose you assign the value returned by data() to the pointer str. In this case, the first condition means str + 0 is valid.
Constructors That Use C-Style Strings
Constructors that use C-style strings let you initialize a string object from a C-style string; more generally, they let you initialize a charT specialization from an array of charT values:
basic_string(const charT* s, const Allocator& a = Allocator());
To determine how many characters to copy, the constructor applies the traits::length() method to the array pointed to by s. (The pointer s should not be a null pointer.) For example, the following statement initializes the toast object, using the indicated character string:
string toast("Here's looking at you, kid.");
The traits::length() method for type char uses the null character to determine how many characters to copy.
The following relationships hold after the constructor is called:
• The data() method returns a pointer to the first element of a copy of the array s.
• The size() method returns a value equal to traits::length().
• The capacity() method returns a value at least as large as size().
Constructors That Use Part of a C-Style String
Constructors that use part of a C-style string let you initialize a string object from part of a C-style string; more generally, they let you initialize a charT specialization from part of an array of charT values:
basic_string(const charT* s, size_type n, const Allocator& a = Allocator());
This constructor copies to the constructed object a total of n characters from the array pointed to by s. Note that it doesn’t stop copying if s has fewer characters than n. If n exceeds the length of s, the constructor interprets the contents of memory following the string as if they held data of type charT.
This constructor requires that s is not a null pointer and that n < npos. (Recall that npos is a static class constant equal to the maximum possible number of elements in a string.) If n equals npos, the constructor throws an out_of_range exception. (Because n is of type size_type and npos is the maximum size_type value, n cannot be greater than npos.) Otherwise, the following relationships hold after the constructor is called:
• The data() method returns a pointer to the first element of a copy of the array s.
• The size() method returns n.
• The capacity() method returns a value at least as large as size().
Constructors That Use an Lvalue Reference
The copy constructor looks like this:
basic_string(const basic_string& str);
It initializes a new string object using a string argument:
string mel("I'm ok!");
string ida(mel);
Here, ida would get a copy of the string managed by mel.
The next constructor additionally requires that you specify an allocator:
basic_string(const basic_string& str, const Allocator&);
The following relationships hold after either of these two constructors is called:
• The data() method returns a pointer to an allocated copy of the array whose first element is pointed to by str.data().
• The size() method returns the value of str.size().
• The capacity() method returns a value at least as large as size().
Moving along, the next constructor lets you set several items:
basic_string(const basic_string& str, size_type pos, size_type n = npos,
const Allocator& a = Allocator());
The second argument pos specifies a location in the source string from which to begin the copying:
string att("Telephone home.");
string et(att, 4);
Position numbers begin with 0, so position 4 is the p character. Thus, et is initialized to "phone home.".
The optional third argument n specifies the maximum number of characters to copy. Thus, this initializes pt to the string "phone":
string att("Telephone home.");
string pt(att, 4, 5);
However, this constructor does not go past the end of the source string; for example, the following stops after copying the period:
string pt(att, 4, 200)
Thus, the constructor actually copies a number of characters equal to the lesser of n and str.size() - pos.