basic_string::basic_string
Constructs a string that is empty or initialized by specific characters, or that is a copy of all or part of some other string object or C-string.
basic_string(
const value_type* _Ptr,
size_type _Count,
const allocator_type& _Al = Allocator ( )
);
basic_string(
const value_type* _Ptr,
const allocator_type& _Al = Allocator ( )
);
basic_string(
const basic_string& _Right,
size_type _Roff,
size_type _Count = npos,
const allocator_type& _Al = Allocator ( )
);
basic_string(
size_type _Count,
value_type _Ch,
const allocator_type& _Al = Allocator ( )
);
explicit basic_string(
const allocator_type& _Al = Allocator ( )
);
template<class _It>
basic_string(
_It _First,
_It _Last,
const allocator_type& _Al = Allocator ( )
);
basic_string(
const_pointer _First,
const_pointer _Last
);
basic_string(
const_iterator _First,
const_iterator _Last
);
Parameters
_Ptr
The C-string whose characters are to be used to initialize the string being constructed. This value cannot be a null pointer._Al
The storage allocator class to be used for the string object being constructed, which defaults to Allocator._Count
The number of characters to be initialized._Right
The string whose characters are to be used to initialize the string being constructed._Roff
The index of a character in a string that is the first to be used to initialize character values for the string being constructed._Ch
The character value to be copied into the string being constructed._First
An input iterator, const_pointer, or const_iterator addressing the first element in the source range to be inserted._Last
An input iterator, const_pointer, or const_iterator addressing the position of the one beyond the last element in the source range to be inserted.
Return Value
A reference to the string object that is being constructed by the member functions.
Remarks
The constructors for class basic_string create and initialize strings as follows:
The first and second constructors create a string that is initialized by all or part of a C-string.
The third member function creates a string that is initialized by all or part of an object of type basic_string.
The fourth member function creates a string that is initialized by a specific number of characters of a parameter stipulated value.
The fifth member function creates an empty string.
Example
// 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.
Requirements
Header: <string>
Namespace: std