CSDN博客

img gaoliang0807

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

发表于2008/9/29 20:28:00  242人阅读

#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

阅读全文
0 0

相关文章推荐

img
取 消
img