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.
要求
**标题:**string
命名空间: std