共用方式為


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
    C 字串的字元是用來初始化string所建構。 此值不可為 null 指標。

  • _Al
    要建構的字串物件儲存配置器類別。

  • _Count
    若要初始化的字元數目。

  • _Right
    要初始化建構字串的字串。

  • _Roff
    是要用來初始化建構字串的字元值的第一個字串中字元的索引。

  • _Ch
    若要複製到要建構字串字元的值。

  • _First
    輸入的 iterator,const_pointer 或 const_iterator 定址來源範圍中的第一個項目插入。

  • _Last
    輸入的 iterator、 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.

需求

標頭: <string>

Namespace: 標準

請參閱

參考

basic_string Class

<string>

值和 Rvalues

其他資源

basic_string 成員

<string> 成員