Game Development Reference
In-Depth Information
Working with Strings
Once you have your data in a string object there are many different methods provided by basic_string
that operate on the internal string data. The first set of useful functions allows you to ask and alter the
string's size and capacity. Table 13-1 shows the different methods supplied to work with the size of
your string data.
Table 13-1. basic_string Capacity Methods
Method
Description
n=str.size()
The number of letter elements in the string.
n=str.length()
Returns the same value as size() .
n=str.max_size()
The maximum possible string length.
str.resize(n, c)
Resizes the string. If the size is shorter than before, trailing characters are lost. If the
size is longer than before, the new elements are initialized to the supplied character.
str.resize(n)
Calls str.resize(n, '\0')
n=str.capacity()
Returns the amount of memory allocated to store the string.
str.reserve(n)
Asks the string to increase the capacity to at least the supplied value.
str.shrink_to_fit()
Asks the string to reduce the capacity to just large enough to fit the size of the string.
Some implementations of basic_string allocate more memory than is needed in case
you try to add more to the string later.
str.clear()
Removes the current string data and reduces size to 0 but might leave capacity
unchanged.
b=str.empty()
Returns true if size() == 0 .
Note: str is a string, n is a number, c is a single character, and b is a boolean.
This set of methods allows you to determine the size of your strings and the amount of memory
being used by your string data. They also allow you to modify the memory requirements of your
strings. Table 13-2 shows the methods you can use to modify your strings.
Table 13-2. basic_string Modifiers
Method
Description
str&=str.append(str)
Append a string to the current string.
str&=str.append(str, pos, n)
Append a substring starting at pos and n length to a string.
str&=str.append(cstr*)
Append a C-style string literal to a string.
str&=str.append(cstr*, n)
Append n characters from a C-style string.
Note: str is a string, cstr is a C-style string, c is a single character, and n is a number.
 
 
Search WWH ::




Custom Search