Chinaunix首页 | 论坛 | 博客
  • 博客访问: 359773
  • 博文数量: 100
  • 博客积分: 2500
  • 博客等级: 大尉
  • 技术积分: 1209
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-15 21:24
文章分类

全部博文(100)

文章存档

2011年(100)

分类: C/C++

2011-04-21 13:43:11


  1. size_t size() const;      //Returns a count of the number of characters in the string.
  1. size_t length() const;    //Returns a count of the number of characters in the string.
  1. size_t max_size ( ) const;
  2. //Returns the maximum number of characters that the  object can hold.
  1. void resize ( size_t n, char c );
  2. void resize ( size_t n );
  3. /*
  4. Resizes the string content to n characters.

    If n is smaller than the current length of the string, the content is reduced to its first n characters, the rest being dropped.

    If n is greater than the current length of the string, the content is expanded by appending as many instances of the c character as needed to reach a size of n characters.

    The second version, actually calls: resize(n,char()), so when a string is resized to a greater size without passing a second argument, the new character positions are filled with the default value of a char, which is the null character.
  5. */
  1. size_t capacity ( ) const; //Returns the size of the allocated storage space in the  object.
  1. void reserve ( size_t res_arg=0 );
  2. //Requests that the  of the allocated storage space in the string be at least res_arg.
  1. void clear();
  2. //The string content is set to an empty string, erasing any previous content and thus leaving its  at 0 characters.
  1. bool empty ( ) const; //Returns whether the  is empty
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;

  4. int
  5. main(void)
  6. {
  7.         size_t sz;
  8.         string str ("Hello");
  9.         cout << "size: " << str.size() << endl;
  10.         cout << "length: " << str.length() << endl;
  11.         cout << "max_size: " << str.max_size() << endl;
  12.         cout << "capacity: " << str.capacity() << endl;

  13.         sz = str.size();
  14.         str.resize(sz+2,'++');
  15.         cout << str << endl;

  16.         str.resize(3);
  17.         cout << str << endl;

  18.         str.clear();
  19.         cout << str << endl;

  20.         if (str.empty()) {
  21.                 cout << "The string is empty now" << endl;
  22.         }

  23.         return (0);
  24. }


阅读(1132) | 评论(0) | 转发(0) |
0

上一篇:string II Iterator

下一篇:String IV Element access

给主人留下些什么吧!~~