P
pass86
#include <iostream>
#include <string>
int main()
{
using namespace std;
string empty;
string small = "bit";
string larger = "Elephants are a girl's best firend";
cout << "Size:\n";
cout << "\tempty: " << empty.size() << endl;
cout << "\tsmall: " << small.size() << endl;
cout << "\tlarger: " << larger.size() << endl;
cout << "Capactites:\n";
cout << "\tempty: " << empty.capacity() << endl;
cout << "\tsmall: " << small.capacity() << endl;
cout << "\tlarger: " << larger.capacity() << endl;
empty.reserve(50);
cout << "Capacity after emtpy.reserve(50): ";
cout << empty.capacity() << endl;
return 0;
}
I get result:
Size:
empty: 0
small: 3
larger: 34
Capactites:
empty: 0
small: 3
larger: 34
Capacity after emtpy.reserve(50): 50
It is different from 《C++ Primer plus 5th》16.4str2.cpp.
#include <string>
int main()
{
using namespace std;
string empty;
string small = "bit";
string larger = "Elephants are a girl's best firend";
cout << "Size:\n";
cout << "\tempty: " << empty.size() << endl;
cout << "\tsmall: " << small.size() << endl;
cout << "\tlarger: " << larger.size() << endl;
cout << "Capactites:\n";
cout << "\tempty: " << empty.capacity() << endl;
cout << "\tsmall: " << small.capacity() << endl;
cout << "\tlarger: " << larger.capacity() << endl;
empty.reserve(50);
cout << "Capacity after emtpy.reserve(50): ";
cout << empty.capacity() << endl;
return 0;
}
I get result:
Size:
empty: 0
small: 3
larger: 34
Capactites:
empty: 0
small: 3
larger: 34
Capacity after emtpy.reserve(50): 50
It is different from 《C++ Primer plus 5th》16.4str2.cpp.