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

Figure 18.5. Printing string characteristics.

(This item is displayed on pages 892 - 893 in the print version)

1 // Fig. 18.5: Fig18_05.cpp 2 // Demonstrating member functions related to size and capacity. 3 #include 4 using std::cout; 5 using std::endl; 6 using std::cin; 7 using std::boolalpha; 8 9 #include 10 using std::string; 11 12 void printStatistics( const string & ); 13 14 int main() 15 { 16 string string1; 17 18 cout << "Statistics before input: " << boolalpha; 19 printStatistics( string1 ); 20 21 // read in only "tomato" from "tomato soup" 22 cout << " Enter a string: "; 23 cin >> string1; // delimited by whitespace 24 cout << "The string entered was: " << string1; 25 26 cout << " Statistics after input: "; 27 printStatistics( string1 ); 28 29 // read in "soup" 30 cin >> string1; // delimited by whitespace 31 cout << " The remaining string is: " << string1 << endl; 32 printStatistics( string1 ); 33 34 // append 46 characters to string1 35 string1 += "1234567890abcdefghijklmnopqrstuvwxyz1234567890"; 36 cout << " string1 is now: " << string1 << endl; 37 printStatistics( string1 ); 38 39 // add 10 elements to string1 40 string1.resize( string1.length() + 10 ); 41 cout << " Stats after resizing by (length + 10): "; 42 printStatistics( string1 ); 43 44 cout << endl; 45 return 0; 46 } // end main 47 48 // display string statistics 49 void printStatistics( const string &stringRef ) 50 { 51 cout << "capacity: " << stringRef.capacity() << " max size: " 52 << stringRef.max_size() << " size: " << stringRef.size() 53 << " length: " << stringRef.length() 54 << " empty: " << stringRef.empty(); 55 } // end printStatistics  

Statistics before input: capacity: 0 max size: 4294967293 size: 0 length: 0 empty: true Enter a string: tomato soup The string entered was: tomato Statistics after input: capacity: 15 max size: 4294967293 size: 6 length: 6 empty: false The remaining string is: soup capacity: 15 max size: 4294967293 size: 4 length: 4 empty: false string1 is now: soup1234567890abcdefghijklmnopqrstuvwxyz1234567890 capacity: 63 max size: 4294967293 size: 50 length: 50 empty: false Stats after resizing by (length + 10): capacity: 63 max size: 4294967293 size: 60 length: 60 empty: false  


The program declares empty string string1 (line 16) and passes it to function printStatistics (line 19). Function printStatistics (lines 4955) 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 length) and whether the string is empty (using member function empty). The initial call to printStatistics indicates that the initial values for the capacity, size and length of string1 are 0.

The size and length of 0 indicate that there are no characters stored in string. Because the initial capacity is 0, when characters are placed in string1, memory is allocated to accommodate the new characters. Recall that the size and length are always identical. In this implementation, the maximum size is 4294967293. Object string1 is an empty string, so function empty returns TRue.

Line 23 reads a string from the command line. 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 27 calls function printStatistics to output statistics for string1. Notice in the output that the length is 6 and that the capacity is 15.

Performance Tip 18.1

To minimize the number of times memory is allocated and deallocated, some string class implementations provide a default capacity above and beyond the length of the string.

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

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

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

Категории