C++ Deck 9 Flashcards
Which library of the Standard Template Library pertains to strings?
<string>
</string>
How are strings declared in <string> compared to without it?</string>
They can be declared as regular variables instead of arrays.
What are four functionalities supported by the <string> library?</string>
- Use of the assignment operator (=)
- Comparison operators (==, !=, etc.)
- The + operator for concatenation
- Type conversions from c-strings to string objects
Is “string” a C++ keyword?
No, it’s an identifier taken from the <string> library.</string>
When comparing strings using the <string> functions, what do the > and < operators do?</string>
They compare two strings and determine which one comes first lexicographically.
“apple”<”apply” Apple comes before apply
“apple”>”Apply” apple comes after Apply
“apple”>”Zebra” apple comes after Zebra
What does <string> class member function:</string>
size()
do?
Returns the length of the string.
What does <string> class member function:</string>
length()
do?
Returns the length of the string (same as size() function)
What does <string> class member function:</string>
capacity()
do?
Returns the current allocated size of the string object. The capacity may be larger than current usage, which is the length()
What does <string> class member function:</string>
resize(X, CH)
do?
Changes the string’s allocated size to X. If X is bigger than the currently stored string, the extra space at the end is filled in with the character in CH.
What does <string> class member function:</string>
clear()
do?
Delete the contents of the string. Reset it to an empty string.
What does <string> class member function:</string>
empty()
do?
Return true if the string is currently empty, false otherwise.
What does <string> class member function:</string>
at(x)
do?
Return the character at position x in the string. Similar to using the [] operator.
e.g.
string.at(0)
is similar to
string[0]
in that they both reference the first character of the string.
What does <string> class member function:</string>
substr(X,Y)
do?
Returns a copy of the substring (i.e. a portion of the original string) that starts at index X and is Y characters long.
Leaving the Y blank will just return the substring starting at index X and going all the way until the end.
What does <string> class member function:</string>
append(str2)
do?
Appends str2 (a string or a c-string)
Appending adds something onto the end of an existing string rather than replacing what’s already there.
What does <string> class member function:</string>
append(str2, Y)
do?
Appends the first Y characters from str2 (a string or character array)