19.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 19.5 demonstrates string class member functions for determining various characteristics of strings.


 1   // Fig. 19.5: Fig19_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   {
11      string string1; // empty string
12
13      cout << "Statistics before input: " << boolalpha;
14      printStatistics( string1 );
15
16      // read in only "tomato" from "tomato soup"
17      cout << " Enter a string: ";
18      cin >> string1; // delimited by whitespace
19      cout << "The string entered was: " << string1;
20
21      cout << " Statistics after input: ";
22      printStatistics( string1 );
23
24      // read in "soup"
25      cin >> string1; // delimited by whitespace
26      cout << " The remaining string is: " << string1 << endl;
27      printStatistics( string1 );
28
29      // append 46 characters to string1
30      string1 += "1234567890abcdefghijklmnopqrstuvwxyz1234567890";
31      cout << " string1 is now: " << string1 << endl;
32      printStatistics( string1 );
33
34      // add 10 elements to string1
35      string1.resize( string1.size() + 10 );
36      cout << " Stats after resizing by (length + 10): ";
37      printStatistics( string1 );
38      cout << endl;
39   } // end main
40
41   // display string statistics
42   void printStatistics( const string &stringRef )
43   {
44      cout << "capacity: " << stringRef.capacity() << " max size: "
45         << stringRef.max_size() << " size: " << stringRef.size()
46         << " length: " << stringRef.size()
47         << " empty: " << stringRef.empty();
48   } // end printStatistics


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


Fig. 19.5. Printing string characteristics.

The program declares empty string string1 (line 11) and passes it to function printStatistics (line 14). Function printStatistics (lines 42–48) 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 18 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 22 calls function printStatistics to output statistics for string1. Notice in the output that the length is 6 and the capacity is 15.

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

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

Line 35 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
18.119.248.149