string类中的size函数和length函数的区别
1个回答
展开全部
测试发现
1. std::string value(2, ‘a’);
结: value.size() == value.length()==2; value.capacity()==31
std::string value(31, ‘a’);
结: value.size() == value.length()==value.capacity()==31;
2. std::string value(32, ‘a’);
结: value.size() == value.length()==32; value.capacity()==63;
std::string value(63, ‘a’);
结: value.size() == value.length()==value.capacity()==63;
3. std::string value(80, ‘a’);
结: value.size()==value.length()==80; value.capacity()==95;
std::string value(95, ‘a’);
结: value.size()==value.length()==value.capactiy()==95;
举3例难发现
a) . size() length() 效C++倾向于用 size();
b) . string容量capactiy(), value值空则capactiy()==0;
否则capacity() 初始值32根据string 存储量变化变化
初始值=32步=32;
1. std::string value(2, ‘a’);
结: value.size() == value.length()==2; value.capacity()==31
std::string value(31, ‘a’);
结: value.size() == value.length()==value.capacity()==31;
2. std::string value(32, ‘a’);
结: value.size() == value.length()==32; value.capacity()==63;
std::string value(63, ‘a’);
结: value.size() == value.length()==value.capacity()==63;
3. std::string value(80, ‘a’);
结: value.size()==value.length()==80; value.capacity()==95;
std::string value(95, ‘a’);
结: value.size()==value.length()==value.capactiy()==95;
举3例难发现
a) . size() length() 效C++倾向于用 size();
b) . string容量capactiy(), value值空则capactiy()==0;
否则capacity() 初始值32根据string 存储量变化变化
初始值=32步=32;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询