MemoryGarden's Blog

努力 -----------大能猫

C++博客 首页 新随笔 联系 聚合 管理
  118 Posts :: 11 Stories :: 20 Comments :: 0 Trackbacks

Returns the size of the allocated storage space in the string object.

Notice that the capacity is not necessarily equal to the number of characters that conform the content of the string (this can be obtained with members size or length), but the capacity of the allocated space, which is either equal or greater than this content size.

Notice also that this capacity does not suppose a limit to the length of the string. If more space is required to accomodate content in the string object, the capacity is automatically expanded, or can even be explicitly modified by calling member reserve.

The real limit on the size a string object can reach is returned by member max_size.

size_t capacity ( ) const;

 1 // comparing size, length, capacity and max_size
 2 #include <iostream>
 3 #include <string>
 4 using namespace std;
 6 int main ()
 7 {
 8   string str ("Test string");
 9   cout << "size: " << str.size() << "\n";
10   cout << "length: " << str.length() << "\n";
11   cout << "capacity: " << str.capacity() << "\n";
12   cout << "max_size: " << str.max_size() << "\n";
13   return 0;
14 }

posted on 2009-12-28 02:21 memorygarden 阅读(355) 评论(0)  编辑 收藏 引用 所属分类: STL

【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理