cplusplus reference–string
cppreference–string
string类对象的容量操作
void test1()
{
string s("hello world");
cout << s << endl;
cout << s.size() << endl;
cout << s.length() << endl;
cout << s.capacity() << endl;
cout << s.max_size() << endl;
s.clear();
cout << s.size() << endl;
cout << s.capacity() << endl;
s.resize(10, 'a');
cout << s << endl;
cout << s.size() << endl;
cout << s.capacity() << endl;
s.resize(15);
cout << s << "over!" << endl;
cout << s.capacity() << endl;
s.resize(5);
cout << s.size() << endl;
cout << s.capacity() << endl;
}
void test2()
{
string s;
s.reserve(100);
cout << s << endl;
cout << s.size() << endl;
cout << s.capacity() << endl;
s.reserve(50);
cout << s << endl;
cout << s.size() << endl;
cout << s.capacity() << endl;
}
void test3()
{
string str = "123";
cout << str.size() << endl;
cout << str.capacity() << endl;
str.reserve(100);
cout << str.size() << endl;
cout << str.capacity() << endl;
str.shrink_to_fit();
cout << str.size() << endl;
cout << str.capacity() << endl;
}