C++ Deck 9 Flashcards

1
Q

Which library of the Standard Template Library pertains to strings?

A

<string>
</string>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How are strings declared in <string> compared to without it?</string>

A

They can be declared as regular variables instead of arrays.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are four functionalities supported by the <string> library?</string>

A
  1. Use of the assignment operator (=)
  2. Comparison operators (==, !=, etc.)
  3. The + operator for concatenation
  4. Type conversions from c-strings to string objects
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Is “string” a C++ keyword?

A

No, it’s an identifier taken from the <string> library.</string>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When comparing strings using the <string> functions, what do the > and < operators do?</string>

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does <string> class member function:</string>

size()

do?

A

Returns the length of the string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does <string> class member function:</string>

length()

do?

A

Returns the length of the string (same as size() function)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does <string> class member function:</string>

capacity()

do?

A

Returns the current allocated size of the string object. The capacity may be larger than current usage, which is the length()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does <string> class member function:</string>

resize(X, CH)

do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does <string> class member function:</string>

clear()

do?

A

Delete the contents of the string. Reset it to an empty string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does <string> class member function:</string>

empty()

do?

A

Return true if the string is currently empty, false otherwise.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does <string> class member function:</string>

at(x)

do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does <string> class member function:</string>

substr(X,Y)

do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does <string> class member function:</string>

append(str2)

do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does <string> class member function:</string>

append(str2, Y)

do?

A

Appends the first Y characters from str2 (a string or character array)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does <string> class member function:</string>

append(str2, X, Y)

do?

A

Appends Y characters from str2, starting at index X

17
Q

What does <string> class member function:</string>

append(X, CH)

do?

A

Appends X copies of the character CH

18
Q

What does <string> class member function:</string>

str.find(str2, X)

do?

A

Returns the first position at or beyond position X where the string ‘str2’ is found inside of ‘str’

19
Q

What does <string> class member function:</string>

str.find(CH, X)

do?

A

Returns the first position at or beyond position X where the character CH is found in ‘str’

20
Q

What does <string> class member function:</string>

str.compare(str2)

do?

A

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.

21
Q

What does <string> class member function:</string>

str.compare(str2, X, Y)

do?

A

Compares the portions of the strings that begin at index X and have length Y.

22
Q

What does <string> class member function:</string>

str.insert(X, Y, CH)

do?

A

Inserts the character CH into string str Y times, starting at position X.

23
Q

What does <string> class member function:</string>

str.insert(X, str2)

do?

A

inserts str2 (string object or char array) into str at position X

24
Q

C-strings pass by _____.

25
C++ string objects pass by _____.
value
26
True or False: C++ string class objects grow their capacity automatically when the number of characters exceeds the allocated space.
True. By contrast, C-strings do NOT do this.
27
What is the value returned by the find() function in the string class?
The index of the position of the first matched character (if the string were matched)
28
Which C++ string class member function returns the internal c-string of the object?
c_str()
29
True or False: The assignment (=) operator can be used to copy C++ string objects and C-strings.
False, it only works for C++ string objects
30
Objects of a class use which operator to access its internal members?
The . operator e.g. string3.find() ^