1 #include<iostream> 2 #include<string> 3 4 using namespace std; 5 6 void Display(const string& str) 7 { 8 cout<<"String: "<<str<<endl; 9 cout<<"Empty: "<<str.empty()<<endl; 10 cout<<"Size: "<<str.size()<<endl; 11 cout<<"Length: "<<str.length()<<endl; 12 cout<<"Capacity: "<<str.capacity()<<endl; 13 cout<<"Maxsize: "<<str.max_size()<<endl; 14 cout<<endl; 15 } 16 17 18 int _tmain(int argc, _TCHAR* argv[]) 19 { 20 string s1=""; //无字节 21 Display(s1); 22 23 string s2=" "; //两个空子节 24 Display(s2); 25 26 string s3="123456"; 27 Display(s3); 28 29 string s4="123 456 asd"; 30 Display(s4); 31 32 /* s3.resize(23); 33 Display(s3);*/ 34 35 system("pause"); 36 return 0; 37 }
结果截图
但成员函数empty()用来检验字符数是否为0,亦即字符串是否为空时比length()或size()来得快。
时间: 2024-10-25 12:50:10