#include <iostream> #include <string> using namespace std; int main () { int size = 0; int length = 0; unsigned long maxsize = 0; int capacity=0; string str ("12345678"); string str_custom; str_custom = str; str_custom.resize (5); size = str_custom.size(); length = str_custom.length(); maxsize = str_custom.max_size(); capacity = str_custom.capacity(); cout << "size = " << size << endl; cout << "length = " << length << endl; cout << "maxsize = " << maxsize << endl; cout << "capacity = " << capacity << endl; return 0; }程式執行結果為:
size = 8
length = 8
maxsize = 2147483647
capacity = 15
str_custom.resize (5);
修改後程式的執行結構如下:
size = 5
length = 5
maxsize = 2147483647
capacity = 15