basic_string::basic_string

构造是空,初始化由特定字符的字符串,或者是另一个字符串对象或 C 样式 (以 null 终止) 字符串复制全部或部分。

basic_string();
explicit basic_string(
    const allocator_type& _Al
);
basic_string(
    const basic_string& _Right
);
basic_string(
    basic_string&& _Right
);
basic_string(
    const basic_string& _Right, 
    size_type _Roff,
    size_type _Count = npos
);
basic_string(
    const basic_string& _Right, 
    size_type _Roff,
    size_type _Count, 
    const allocator_type& _Al
);
basic_string(
    const value_type *_Ptr, 
    size_type _Count
);
basic_string(
    const value_type *_Ptr, 
    size_type _Count,
    const allocator_type& _Al
);
basic_string(
    const value_type *_Ptr
);
basic_string(
    const value_type *_Ptr,
    const allocator_type& _Al
);
basic_string(
    size_type _Count, 
    value_type _Ch
);
basic_string(
    size_type _Count, 
    value_type _Ch,
    const allocator_type& _Al
);
template <class InputIterator>
    basic_string(
        InputIterator _First, 
        InputIterator _Last
    );
template <class InputIterator>
    basic_string(
        InputIterator _First, 
        InputIterator _Last, 
        const allocator_type& _Al
    );
basic_string(
   const_pointer _First,
   const_pointer _Last
);
basic_string(
   const_iterator _First,
   const_iterator _Last
);

参数

  • _Ptr
    字符用于初始化的构造 string 的 C 字符串。 此值不能为 null 指针。

  • _Al
    构造的字符串对象中存储区分配程序类。

  • _Count
    要初始化的字符数。

  • _Right
    初始化字符串构造。

  • _Roff
    一个字符的索引。是使用的第一个初始化构造的字符串的字符值的字符串。

  • _Ch
    要复制的字符值。构造的字符串。

  • _First
    在解决源区的输入迭代器、const_pointer 或 const_iterator 第一个元素将被插入。

  • _Last
    处理位置的集合 const_pointer 输入迭代器、const_iterator 除元素外或最后将插入的源区。

返回值

为由构造函数构造的字符串对象的引用。

备注

所有结构函数 basic_string::allocator_type 存储和初始化控制序列。 分配器对象是从参数al。 对于复制构造函数,为 right.()。basic_string::get_allocator 否则为 Alloc()。

序列控制初始化为其余的操作数指定操作数的序列复制。 没有操作数序列的构造函数指定空的初始序列控制。 如果 InputIterator 为整数输入模板构造函数,操作数序列_First, _Last 的行为与 (size_type)_First, (value_type)_Last相同。

示例

// basic_string_ctor.cpp
// compile with: /EHsc
#include <string>
#include <iostream>

int main( )
{
   using namespace std;

   // The first member function initializing with a C-string
   const char *cstr1a = "Hello Out There.";
   basic_string <char> str1a ( cstr1a , 5);
   cout << "The string initialized by C-string cstr1a is: "
        << str1a << "." << endl;

   // The second member function initializing with a string
   string  str2a ( "How Do You Do?" );
   basic_string <char> str2b ( str2a , 7 , 7 );
   cout << "The string initialized by part of the string cstr2a is: "
        << str2b << "." << endl;

   // The third member function initializing a string
   // with a number of characters of a specific value
   basic_string <char> str3a ( 5, '9' );
   cout << "The string initialized by five number 9s is: "
        << str3a << endl;

   // The fourth member function creates an empty string
   // and string with a specified allocator
   basic_string <char> str4a;
   string str4b;
   basic_string <char> str4c ( str4b.get_allocator( ) );
   if (str4c.empty ( ) )
      cout << "The string str4c is empty." << endl;
   else
      cout << "The string str4c is not empty." << endl;

   // The fifth member function initializes a string from
   // another range of characters
   string str5a ( "Hello World" );
   basic_string <char> str5b ( str5a.begin ( ) + 5 , str5a.end ( ) );
   cout << "The string initialized by another range is: "
        << str5b << "." << endl;
}

Output

The string initialized by C-string cstr1a is: Hello.
The string initialized by part of the string cstr2a is: You Do?.
The string initialized by five number 9s is: 99999
The string str4c is empty.
The string initialized by another range is:  World.

要求

标头:< 字符串>

命名空间: std

请参见

参考

basic_string 类

<string>

Lvalues 和 Rvalues

其他资源

basic_string 的成员

string 成员