4.4获得字符串的长度--4.9 字符串长度和容量

#include<string>
#include<iostream>

using namespace std;
void main()
{
 string s="";
 string str="";
 str.reserve(9000);

 cout<<"s.length  ="<<s.length()<<'/n';
 cout<<"s.capacity ="<<s.capacity()<<'/n';
 cout<<"s.max_size ="<<s.max_size()<<'/n';

 cout<<"str.length  ="<<str.length()<<'/n';
 cout<<"str.capacity ="<<str.capacity()<<'/n';
 cout<<"str.max_size ="<<str.max_size()<<'/n';

 for(int i=0;i<1000;++i)
 {
  if(s.length()==s.capacity())
  {
   cout<<"s reached capacity of"<<s.length()<<",growing.../n";
  }
  if(str.length()==str.capacity())
  {
   cout<<"str reached capacity of"<<str.length()<<",growing.../n";
  }
  s+='x';
  str+='x';
 }
}

输出:

s.length  =0
s.capacity =0
s.max_size =4294967293
str.length  =0
str.capacity =9023
str.max_size =4294967293
s reached capacity of0,growing...
s reached capacity of31,growing...
s reached capacity of63,growing...
s reached capacity of95,growing...
s reached capacity of127,growing...
s reached capacity of159,growing...
s reached capacity of191,growing...
s reached capacity of223,growing...
s reached capacity of255,growing...
s reached capacity of287,growing...
s reached capacity of319,growing...
s reached capacity of351,growing...
s reached capacity of383,growing...
s reached capacity of415,growing...
s reached capacity of447,growing...
s reached capacity of479,growing...
s reached capacity of511,growing...
s reached capacity of543,growing...
s reached capacity of575,growing...
s reached capacity of607,growing...
s reached capacity of639,growing...
s reached capacity of671,growing...
s reached capacity of703,growing...
s reached capacity of735,growing...
s reached capacity of767,growing...
s reached capacity of799,growing...
s reached capacity of831,growing...
s reached capacity of863,growing...
s reached capacity of895,growing...
s reached capacity of927,growing...
s reached capacity of959,growing...
s reached capacity of991,growing...
Press any key to continue

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值