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)
What does <string> class member function:</string>
append(str2, X, Y)
do?
Appends Y characters from str2, starting at index X
What does <string> class member function:</string>
append(X, CH)
do?
Appends X copies of the character CH
What does <string> class member function:</string>
str.find(str2, X)
do?
Returns the first position at or beyond position X where the string ‘str2’ is found inside of ‘str’
What does <string> class member function:</string>
str.find(CH, X)
do?
Returns the first position at or beyond position X where the character CH is found in ‘str’
What does <string> class member function:</string>
str.compare(str2)
do?
Performs a comparison, like the c-string function stcmp. A negative return means str1 comes first. Positive means str2 comes first. 0 means they are the same.
What does <string> class member function:</string>
str.compare(str2, X, Y)
do?
Compares the portions of the strings that begin at index X and have length Y.
What does <string> class member function:</string>
str.insert(X, Y, CH)
do?
Inserts the character CH into string str Y times, starting at position X.
What does <string> class member function:</string>
str.insert(X, str2)
do?
inserts str2 (string object or char array) into str at position X
C-strings pass by _____.
address