0
C++ String Deitel C++ 7th edition; Chapter Class String and String Processing. 18.6 String Characteristic
Hi class. Please let me know, why the value of string.capacity = 15 when I enter string = tomato; and cin>>string = soup (delimited by white space)? and why the value of string1.capacity become 63, when I assign: string1= "soup"; then string1+="1234567890abcdefghijklmnopqrstuvwxyz1234567890"? yield result string1="soup1234567890abcdefghijklmnopqrstuvwxyz1234567890" and string1.capacity = 63. Next, why the value of maximum size is 2^32 - 3? Thank you.
9 Respostas
0
Oliver Pasaribu yeah Its way bigger than 2^32-3.
I'm yet to see an implementation in which max size is that number.(2^32-3)
+ 1
~ swim ~ To correct you, the maximum size of a string is substantially lower than 2^64-1.
try running this and see for yourself
cout<<string{}.max_size();
0
max size: 4294967293 in the textbook might be equal to 4294967296-3. 4294967293 is obtained from the deitel's 7th book. 2^32 will result 4294967296. Intuitively, I think this number 4294967293 is from 2^32 -3. Please give an explanation.
0
Anthony Maina, I run the code you gave me just now, and I obtained 4611686018427387903 is the max size.
0
swim,altough string size, capacity, max_size can dynamically grow or increase, but remember that it size can be calculated. for example string consists of 10 characters array must have a size 10*1 bytes or 1 byte or 1 word or 1 char. So, if maximum character in string is n character, It is valid for us to say that the maximum capacity of the string is n * 1 byte.
0
Anthony Maina; so which one is the right answer? maximum size allowed of such string in 32-bit processor is 2^32 = 4294967296 or 2^32 -1 = 4294967295; or 2^32 - 3 = 4294967293? Why the result of maximum size 4294967293 is displayed? Next, how to explain the reason why the string1.capacity is vary from 15 to 63?
0
Swim, I run these codes using sololearn compiler.
#include <iostream>
using namespace std;
int main() {
string empty = "";
cout << "size = " << empty.size() << '\n';
cout << "capacity = " << empty.capacity() << '\n';
empty += "empty";
cout << "current size = " << empty.size() << '\n';
cout << "current capacity = " << empty.capacity() << '\n';
empty += "abcdefghijklmnopqrst";
cout << "current size = " << empty.size() << '\n';
cout << "current capacity = " << empty.capacity() << '\n';
}
This is the output displayed on the screen:
size = 0
capacity = 15
current size = 5
current capacity = 15
current size = 25
current capacity = 30.
The question is where is the value of empty string="" 15 come from? Default value? And Why the value of capacity is increased from 15 to 30. It is clear that size value indicates the number of characters in the string empty.
0
Oliver Pasaribu The basic_string class uses something called an anonymous union.This union contains 2 members, a char array of size 16 and a size_t variable.
The array is used to store a maximum of 16 characters(15 chars+1 null terminating char).When the number of characters exceeds 16,then the string object acquires more memory from the heap and copies the chars.
This is the reason as to why you're getting the initial capacity of an empty string as 15 since that is the maximum number of chars you can store in the internal array before more memory is allocated from the heap.
Before you ask,the size_t variable is used to store the size of the string once the characters have been copied onto the heap.
0
Swim, Anthony Maina. Please check this code:
// Fig. 18.5: Fig18_05.cpp
// Demonstrating member functions related to size and capacity.
#include <iostream>
#include <string>
using namespace std;
void printStatistics( const string & );
int main() {
string string1;
cout << "Statistics before input:\n" << boolalpha;
printStatistics( string1 );
// read in only "tomato" from "tomato soup"
cout << "\n\nEnter a string: ";
cin >> string1; // delimited by whitespace
cout << "The string entered was: " << string1;
cout << "\nStatistics after input:\n";
printStatistics( string1 );
// read in "soup"
cin >> string1; // delimited by whitespace
cout << "\n\nThe remaining string is: " << string1 << endl;
printStatistics( string1 );
// append 46 characters to string1
string1 += "1234567890abcdefghijklmnopqrstuvwxyz1234567890";
cout << "\n\nstring1 is now: " << string1 << endl;
printStatistics( string1 );
// add 10 elements to string1
string1.resize( string1.length() + 10 );
cout << "\n\nStats after resizing by (length + 10):\n";
printStatistics( string1 );
cout << endl;
return 0;
} // end main
// display string statistics
void printStatistics( const string &stringRef )
{
cout << "capacity: " << stringRef.capacity() << "\nmax size: "
<< stringRef.max_size() << "\nsize: " << stringRef.s
Output on screen:
Statistics before input:
capacity: 15
max size: 4611686018427387903
size: 0
length: 0
empty: true
Enter a string: The string entered was: tomato
Statistics after input:
capacity: 15
max size: 4611686018427387903
size: 6
length: 6
empty: false
The remaining string is: soup
capacity: 15
max size: 4611686018427387903
size: 4
length: 4
empty: false
string1 is now: soup1234567890abcdefghijklmnopqrstuvwxyz1234567890
capacity: 50
max size: 4611686018427387903
size: 50
length: 50
empty: false
Stats after resizing by (length + 10):