std::basic_string<CharT,Traits,Allocator>::basic_string
cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
| (1) | ||
basic_string(); explicit basic_string( const Allocator& alloc ); |
( C++17) | |
basic_string() noexcept(noexcept( Allocator() )) : basic_string( Allocator() ) {} explicit basic_string( const Allocator& alloc ) noexcept; |
( C++17) ( C++20) |
|
constexpr basic_string() noexcept(noexcept( Allocator() )) : basic_string( Allocator() ) {} constexpr explicit basic_string( const Allocator& alloc ) noexcept; |
( C++20) | |
| (2) | ||
basic_string( size_type count, CharT ch, const Allocator& alloc = Allocator() ); |
( C++20) | |
constexpr basic_string( size_type count, CharT ch, const Allocator& alloc = Allocator() ); |
( C++20) | |
| (3) | ||
basic_string( const basic_string& other, size_type pos, const Allocator& alloc = Allocator() ); |
( C++20) | |
constexpr basic_string( const basic_string& other, size_type pos, const Allocator& alloc = Allocator() ); |
( C++20) | |
constexpr basic_string( basic_string&& other, size_type pos, const Allocator& alloc = Allocator() ); |
(3) | ( C++23) |
| (3) | ||
basic_string( const basic_string& other, size_type pos, size_type count, const Allocator& alloc = Allocator() ); |
( C++20) | |
constexpr basic_string( const basic_string& other, size_type pos, size_type count, const Allocator& alloc = Allocator() ); |
( C++20) | |
constexpr basic_string( basic_string&& other, size_type pos, size_type count, const Allocator& alloc = Allocator() ); |
(3) | ( C++23) |
| (4) | ||
basic_string( const CharT* s, size_type count, const Allocator& alloc = Allocator() ); |
( C++20) | |
constexpr basic_string( const CharT* s, size_type count, const Allocator& alloc = Allocator() ); |
( C++20) | |
| (5) | ||
basic_string( const CharT* s, const Allocator& alloc = Allocator() ); |
( C++20) | |
constexpr basic_string( const CharT* s, const Allocator& alloc = Allocator() ); |
( C++20) | |
| (6) | ||
template< class InputIt > basic_string( InputIt first, InputIt last, const Allocator& alloc = Allocator() ); |
( C++20) | |
template< class InputIt > constexpr basic_string( InputIt first, InputIt last, const Allocator& alloc = Allocator() ); |
( C++20) | |
| (7) | ||
basic_string( const basic_string& other ); |
( C++20) | |
constexpr basic_string( const basic_string& other ); |
( C++20) | |
| (7) | ||
basic_string( const basic_string& other, const Allocator& alloc ); |
( C++11) ( C++20) |
|
constexpr basic_string( const basic_string& other, const Allocator& alloc ); |
( C++20) | |
| (8) | ||
basic_string( basic_string&& other ) noexcept; |
( C++11) ( C++20) |
|
constexpr basic_string( basic_string&& other ) noexcept; |
( C++20) | |
| (8) | ||
basic_string( basic_string&& other, const Allocator& alloc ); |
( C++11) ( C++20) |
|
constexpr basic_string( basic_string&& other, const Allocator& alloc ); |
( C++20) | |
| (9) | ||
basic_string( std::initializer_list<CharT> ilist, const Allocator& alloc = Allocator() ); |
( C++11) ( C++20) |
|
constexpr basic_string( std::initializer_list<CharT> ilist, const Allocator& alloc = Allocator() ); |
( C++20) | |
| (10) | ||
template< class StringViewLike > explicit basic_string( const StringViewLike& t, const Allocator& alloc = Allocator() ); |
( C++17) ( C++20) |
|
template< class StringViewLike > constexpr explicit basic_string( const StringViewLike& t, const Allocator& alloc = Allocator() ); |
( C++20) | |
| (11) | ||
template< class StringViewLike > basic_string( const StringViewLike& t, size_type pos, size_type n, const Allocator& alloc = Allocator() ); |
( C++17) ( C++20) |
|
template< class StringViewLike > constexpr basic_string( const StringViewLike& t, size_type pos, size_type n, const Allocator& alloc = Allocator() ); |
( C++20) | |
basic_string( std::nullptr_t ) = delete; |
(12) | ( C++23) |
template< container-compatible-range<CharT> R > constexpr basic_string( std::from_range_t, R&& rg, const Allocator& = Allocator()); |
(13) | ( C++23) |
, , alloc.
1) . ( ). , , .
3)
[pos, pos + count) other. count == npos, count , [pos, other.size()). other rvalue, , . ( C++23)4)
count , s. s . count. , [s, s + count) .6)
[first, last).
|
|
( C++11) |
|
, |
( C++11) |
7) .
other.8) .
other, . other , .9)
ilist. 10)
t sv, std::basic_string_view<CharT, Traits> sv = t;, sv, basic_string(sv.data(), sv.size(), alloc). ,
std::is_convertible_v<const StringViewLike&,std::basic_string_view<CharT, Traits>> true std::is_convertible_v<const StringViewLike&, const CharT*> false.
11)
t sv, std::basic_string_view<CharT, Traits> sv = t;, [pos, pos + n) sv, basic_string(sv.substr(pos, n), alloc). ,
std::is_convertible_v<const StringViewLike&,std::basic_string_view<CharT, Traits>> true.
12)
std::basic_string nullptr.13) ,
rg.| alloc | , | |
| count | ||
| ch | ||
| pos | , | |
| first, last | ||
| s | , | |
| other | , | |
| ilist | std::initializer_list | |
| t | ( std::basic_string_view) | |
| rg | no section name |
1) .
2-4)
count.5)
s.6)
first last.7)
other.8) .
alloc alloc != other.get_allocator(), .9)
ilist.13)
rg.8) ,
alloc == str.get_allocator(). std::length_error, max_size() (, count > max_size() (2)). Allocator::allocate .
, '\0', (5), . , operator""s:
std::string s1 = "ab\0\0cd"; // s1 "ab"
std::string s2{"ab\0\0cd", 6}; // s2 "ab\0\0cd"
std::string s3 = "ab\0\0cd"s; // s3 "ab\0\0cd"
__cpp_lib_containers_ranges |
202202L |
(C++23) | (13) no section name |
#include <cassert>
#include <cctype>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <string>
int main()
{
std::cout << "1) string(); ";
std::string s1;
assert(s1.empty() && (s1.length() == 0) && (s1.size() == 0));
std::cout << "s1.capacity(): " << s1.capacity() << '\n'; //
std::cout << "2) string(size_type count, CharT ch): ";
std::string s2(4, '=');
std::cout << std::quoted(s2) << '\n'; // "===="
std::cout << "3) string(const string& other, size_type pos, size_type count): ";
std::string const other3("");
std::string s3(other3, 0, other3.length() - 1);
std::cout << std::quoted(s3) << '\n'; // ""
std::cout << "4) string(const string& other, size_type pos): ";
std::string const other4("Mutatis Mutandis");
std::string s4(other4, 8);
std::cout << std::quoted(s4) << '\n'; // "Mutandis", [8, 16)
std::cout << "5) string(CharT const* s, size_type count): ";
std::string s5("C-style", 7);
std::cout << std::quoted(s5) << '\n'; // "C-style", [0, 7)
std::cout << "6) string(CharT const* s): ";
std::string s6("\0 ");
std::cout << std::quoted(s6) << '\n'; // ""
std::cout << "7) string(InputIt first, InputIt last): ";
char mutable_c_str[] = "another C-style string";
std::string s7(std::begin(mutable_c_str) + 8, std::end(mutable_c_str) - 1);
std::cout << std::quoted(s7) << '\n'; // " C"
std::cout << "8) string(string&): ";
std::string const other8("");
std::string s8(other8);
std::cout << std::quoted(s8) << '\n'; // ""
std::cout << "9) string(string&&): ";
std::string s9(std::string("++ ") + std::string(""));
std::cout << std::quoted(s9) << '\n'; // "++ "
std::cout << "a) string(std::initializer_list<CharT>): ";
std::string sa({'C', '-', 's', 't', 'y', 'l', 'e'});
std::cout << std::quoted(sa) << '\n'; // "C-style"
// C++11 string(InputIt first, InputIt last)
// [ InputIt = int], * *
// string(size_type count, CharT ch).
// C++11 InputIt :
std::cout << "b) string(size_type count, CharT ch) : ";
std::string sb(3, std::toupper('a'));
std::cout << std::quoted(sb) << '\n'; // "AAA"
// std::string sc(nullptr); // C++23: std::logic_error
// C++23: ,
// (12)
// std::string sc(0); // , , 0
//
auto const range = {0x43, 43, 43};
#ifdef __cpp_lib_containers_ranges
std::string sc(std::from_range, range); // (13)
std::cout << "c) string(std::from_range, range) : ";
#else
std::string sc(range.begin(), range.end()); // (7)
std::cout << "c) string(range.begin(), range.end()) : ";
#endif
std::cout << std::quoted(sc) << '\n'; // "C++"
}:
1) string(); s1.capacity(): 15
2) string(size_type count, CharT ch): "===="
3) string(const string& other, size_type pos, size_type count): ""
4) string(const string& other, size_type pos): "Mutandis"
5) string(CharT const* s, size_type count): "C-style"
6) string(CharT const* s): ""
7) string(InputIt first, InputIt last): "C-style string"
8) string(string&): ""
9) string(string&&): "++ "
a) string(std::initializer_list<CharT>): "C-style"
b) string(size_type count, CharT ch) : "AAA"
c) string(std::from_range, range) : "C++"C++:
| LWG 301 | C++98 | (6) alloc, InputIt
|
|
| LWG 847 | C++98 | ||
| LWG 2193 | C++11 | ||
| LWG 2583 | C++98 | basic_string(str, pos)
|
basic_string(str, pos, alloc)
|
| LWG 2946 | C++17 | (10) |
, |
| LWG 3076 | C++17 | |
See also
| (public -) | |
| (public -) | |
(C++11) |
string () |
(C++11) |
wstring () |
(C++17) |
basic_string_view (public - std::basic_string_view<CharT,Traits>)
|