在 Visual C++ 演示如何使用 basic_string:: 范围 和 basic_string:: 调整 标准 (STL)模板库函数。
size_type size( ) const;
   void resize(
      size_type n, 
      E c = E( )
   );
备注
 说明 | 
|---|
类/参数名在原型不匹配版本在头文件。修改某些提高可读性。  | 
basic_string::size STL 函数返回序列的长度。为长度范围由第一个参数指定的 basic_string::resize STL 函数。如果该顺序使较长,该函数追加了第二个参数的值的元素。此值默认为 null。代码示例的输出显示 null 字符的空间。operatorAMP_LTAMP_LT 读取字符串和输出的范围在字符串中的每个字符一个节点。
示例
// size.cpp
// compile with: /EHsc
// 
// Functions:
//    size()
//    resize() ; Defined in header xstring which is included indirectly.
//////////////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
using namespace std;
int main()
{
   string TestString = "1111122222333334444455555";
   cout << "[" << TestString << "]" << endl
        << "size: " << TestString.size() << endl
        << endl;
   TestString.resize(5);
   cout << "[" << TestString << "]" << endl
        << "size: " << TestString.size() << endl
        << endl;
   TestString.resize(10);
   cout << "[" << TestString << "]" << endl
        << "size: " << TestString.size() << endl
        << endl;
   TestString.resize(15,'6');
   cout << "[" << TestString << "]" << endl
        << "size: " << TestString.size() << endl;
}
示例输出
[1111122222333334444455555]
size: 25
[11111]
size: 5
[11111     ]
size: 10
[11111     66666]
size: 15
要求
**标题:**string
说明