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
輸入 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>
命名空間: std