string属性

 #include<iostream>

#include<string>

using namespace std;

int main(){

         string str;                    //使用属性测容量str.capacity()

         cout<<str.capacity()<<endl;

        

         string str1(5,'a');              //少于15个,至少申请个15容量

         cout<<str1.capacity()<<endl;     //以后多余容量就增加16个大小

         str1.resize(3);                   //str.resize(3)重置字符个数,容量不变

         cout<<str1<<endl;

         cout<<str1.length()<<endl;        //str.length()测字符串长度

         cout<<str1.size()<<endl;           //str.size()测字符个数

         cout<<str1.capacity()<<endl;

        

         string str2("abcde",3);          //VC6.0是至少申请31个,以后每次增加32个

         cout<<str2.capacity()<<endl;

        

         system("pause");

         return 0;

}

原文地址:https://www.cnblogs.com/huoyuying/p/9662626.html