您的位置:首页 > 其它

string的empty,size,length等比较

2015-11-18 18:19 405 查看
#include<iostream>
#include<string>

using namespace std;

void Display(const string& str)
{
cout<<"String: "<<str<<endl;
cout<<"Empty: "<<str.empty()<<endl;
cout<<"Size: "<<str.size()<<endl;
cout<<"Length: "<<str.length()<<endl;
cout<<"Capacity: "<<str.capacity()<<endl;
cout<<"Maxsize: "<<str.max_size()<<endl;
cout<<endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
string s1="";  //无字节
Display(s1);

string s2="  ";  //两个空子节
Display(s2);

string s3="123456";
Display(s3);

string s4="123 456 asd";
Display(s4);

/* s3.resize(23);
Display(s3);*/

system("pause");
return 0;
}


结果截图



但成员函数empty()用来检验字符数是否为0,亦即字符串是否为空时比length()或size()来得快。



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: