admin管理员组文章数量:1612060
1. string类的capacity/max_size/size/length/empty/reserve/resize
1.1 std::string::capacity
- 原型: size_t capacity() const;
- 说明: 返回string分配的存储容量。
1.2 std::string::max_size
- 原型: size_t max_size() const;
- 说明: 返回string对象中可存放的最大字符串的长度。
1.3 std::string::size
- 原型: size_t size() const;
- 说明: 返回源字符串的长度大小。
- 注意:size()与length()方法的功能一样,都是计算能够输出的可见字符的长度 ,length()是早期的版本,size()是后来添加的为了兼容STL。
1.4 std::string::length
- 原型: size_t length() const;
- 说明: 返回源字符串的长度大小。
- 注意:size()与length()方法的功能一样,都是计算能够输出的可见字符的长度 ,length()是早期的版本,size()是后来添加的为了兼容STL。
代码示例:capacity/max_size/size/length
#include <iostream> #include <string> using namespace std; int main () { string str ("Test string"); cout<<"capacity: "<<str.capacity()<<endl; cout<<"max_size: "<<str.max_size()<<endl; cout<<"size: "<<str.size()<<endl; cout<<"length: "<<str.length()<<endl; system("pause"); return 0; } =>capacity: 15 max_size: 4294967294 size: 11 length: 11
1.5 std::string::empty
- 原型: bool empty() const;
- 说明: 判断源字符串是否为空。
代码示例:empty
#include <iostream> #include <string> using namespace std; int main () { string content; string line; cout<<"Please introduce a text. Enter an empty line to finish:"<<endl; do { getline(std::cin, line); content += line + '\n'; }while (!line.empty()); cout <<"The text you introduced was:\n"<<content; system("pause"); return 0; } =>Please introduce a text. Enter an empty line to finish: The text you introduced was:
1.7 std::string::reserve
- 原型: void reserve (size_t n = 0);
- 说明: 重新给源字符串分配存储最小为n的容量。
- 注意:指定容器能存储数据的个数,将字符串的容量设置为至少n,string增加容量时,每次增加16的倍数。
代码示例:reserve/capacity/max_size/size/length
#include <iostream> #include <string> using namespace std; int main () { string str ("Test string"); cout<<"调用str.reserve(size_t n=0);之前:"<<endl; cout<<"capacity: "<<str.capacity()<<endl; cout<<"max_size: "<<str.max_size()<<endl; cout<<"size: "<<str.size()<<endl; cout<<"length: "<<str.length()<<endl; cout<<endl; str.reserve(50);//string增加容量时,每次增加16的倍数, cout<<"str.reserve(50);之后:str.capacity()=16*4-1"<<endl; cout<<"capacity: "<<str.capacity()<<endl;//16*4-1 cout<<"max_size: "<<str.max_size()<<endl; cout<<"size: "<<str.size()<<endl; cout<<"length: "<<str.length()<<endl; cout<<endl; str.reserve(100);//string增加容量时,每次增加16的倍数 cout<<"str.reserve(100);之后:str.capacity()=16*7-1"<<endl; cout<<"capacity: "<<str.capacity()<<endl; cout<<"max_size: "<<str.max_size()<<endl;//16*7-1 cout<<"size: "<<str.size()<<endl; cout<<"length: "<<str.length()<<endl; cout<<endl; system("pause"); return 0; } =>调用str.reserve(size_t n=0);之前: capacity: 15 max_size: 4294967294 size: 11 length: 11 str.reserve(50);之后:str.capacity()=16*4-1 capacity: 63 max_size: 4294967294 size: 11 length: 11 str.reserve(100);之后:str.capacity()=16*7-1 capacity: 111 max_size: 4294967294 size: 11 length: 11
1.7 std::string::resize
- 原型: void resize (size_t n);
- 说明: 调整源字符串的长度为n。
- 原型: void resize (size_t n, char c);
- 说明: 调整字符串长度为n,并用字符c填充不足的部分 。
代码示例:
#include <iostream> #include <string> using namespace std; int main () { string str ("I like to code in C"); cout<<str<<endl; unsigned sz = str.size(); str.resize(sz+2,'+'); cout<<str<<endl; str.resize(14); cout<<str<<endl; system("pause"); return 0; } =>I like to code in C I like to code in C++ I like to code
参考文献:
[1] 网络资源: http://www.cplusplus/reference/string/string/capacity/
http://www.cplusplus/reference/string/string/max_size/
http://www.cplusplus/reference/string/string/size/
http://www.cplusplus/reference/string/string/length/
http://www.cplusplus/reference/string/string/empty/
http://www.cplusplus/reference/string/string/reserve/
http://www.cplusplus/reference/string/string/resize/
本文标签: 基础CapacitymaxsizesizeString
版权声明:本文标题:C++基础---string类的capacitymax_sizesizelengthemptyreserveresize 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dongtai/1728621365a1166399.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论