21.6 string Characteristics

Class string provides member functions for gathering information about a string’s size, length, capacity, maximum length and other characteristics. A string’s size or length is the number of characters currently stored in the string. A string’s capacity is the number of characters that can be stored in the string without allocating more memory. The capacity of a string must be at least equal to the current size of the string, though it can be greater. The exact capacity of a string depends on the implementation. The maximum size is the largest possible size a string can have. If this value is exceeded, a length_error exception is thrown. Figure 21.5 demonstrates string class member functions for determining various characteristics of strings.

Fig. 21.5 Printing string characteristics.

Alternate View

 1  // Fig. 21.5: Fig21_05.cpp
 2  // Printing string characteristics.
 3  #include <iostream>
 4  #include <string>
 5  using namespace std;
 6
 7  void printStatistics(const string&);
 8
 9   int main() {
10      string string1; // empty string
11
12      cout << "Statistics before input:
" << boolalpha;
13      printStatistics(string1);
14
15      // read in only "tomato" from "tomato soup"
16      cout << "

Enter a string: ";
17      cin >> string1; // delimited by whitespace
18      cout << "The string entered was: " << string1;
19
20      cout << "
Statistics after input:
";
21      printStatistics(string1);
22
23      // read in "soup"
24      cin >> string1; // delimited by whitespace
25      cout << "

The remaining string is: " << string1 << endl;
26      printStatistics(string1);
27
28      // append 46 characters to string1
29      string1 += "1234567890abcdefghijklmnopqrstuvwxyz1234567890";
30      cout << "

string1 is now: " << string1 << endl;
31      printStatistics(string1);
32
33      // add 10 elements to string1
34      string1.resize(string1.size() + 10);
35      cout << "

Stats after resizing by (length + 10):
";
36      printStatistics(string1);
37      cout << endl;
38  }
39
40  // display string statistics
41  void printStatistics(const string& stringRef) {
42     cout << "capacity: " << stringRef.capacity() << "
max size: "
43        << stringRef.max_size() << "
size: " << stringRef.size()
44        << "
length: " << stringRef.size()
45        << "
empty: " << stringRef.empty();
46  }

Statistics before input:
capacity: 15
max size: 4294967294
size: 0
length: 0
empty: true

Enter a string: tomato soup
The string entered was: tomato
Statistics after input:
capacity: 15
max size: 4294967294

size: 6
length: 6
empty: false

The remaining string is: soup
capacity: 15
max size: 4294967294
size: 4
length: 4
empty: false

string1 is now: soup1234567890abcdefghijklmnopqrstuvwxyz1234567890
capacity: 63
max size: 4294967294
size: 50
length: 50
empty: false

Stats after resizing by (length + 10):
capacity: 63
max size: 4294967294
size: 60
length: 60
empty: false

The program declares empty string string1 (line 10) and passes it to function printStatistics (line 13). Function printStatistics (lines 41–46) takes a reference to a const string as an argument and outputs the capacity (using member function capacity), maximum size (using member function max_size), size (using member function size), length (using member function size) and whether the string is empty (using member function empty). The initial call to printStatistics indicates that the initial values for the size and length of string1 are 0.

The size and length of 0 indicate that there are no characters stored in string. Recall that the size and length are always identical. In this implementation, the maximum size is 4,294,967,294. Object string1 is an empty string, so function empty returns true.

Line 17 inputs a string. In this example, "tomato soup" is input. Because a space character is a delimiter, only "tomato" is stored in string1; however, "soup" remains in the input buffer. Line 21 calls function printStatistics to output statistics for string1. Notice in the output that the length is 6 and the capacity is 15.

Line 24 reads "soup" from the input buffer and stores it in string1, thereby replacing "tomato". Line 27 passes string1 to printStatistics.

Line 29 uses the overloaded += operator to concatenate a 46-character-long string to string1. Line 31 passes string1 to printStatistics. The capacity has increased to 63 elements and the length is now 50.

Line 34 uses member function resize to increase the length of string1 by 10 characters. The additional elements are set to null characters. The output shows that the capacity has not changed and the length is now 60.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset