std::basic_string<CharT,Traits,Allocator>:: basic_string
| (1) | ||
|
basic_string
(
)
:
basic_string
(
Allocator
(
)
)
{
}
|
(seit C++11)
(bis C++17) |
|
|
basic_string
(
)
noexcept
(
noexcept
(
Allocator
(
)
)
)
: basic_string ( Allocator ( ) ) { } |
(seit C++17)
(constexpr seit C++20) |
|
| (2) | ||
|
explicit
basic_string
(
const
Allocator
&
alloc
=
Allocator
(
)
)
;
|
(bis C++11) | |
|
explicit
basic_string
(
const
Allocator
&
alloc
)
;
|
(noexcept seit C++17)
(constexpr seit C++20) |
|
|
basic_string
(
size_type count, CharT ch,
const Allocator & alloc = Allocator ( ) ) ; |
(3) | (constexpr seit C++20) |
|
template
<
class
InputIt
>
basic_string
(
InputIt first, InputIt last,
|
(4) | (constexpr seit C++20) |
|
template
<
container-compatible-range
<
CharT
>
R
>
constexpr
basic_string
(
std::
from_range_t
, R
&&
rg,
|
(5) | (seit C++23) |
|
basic_string
(
const
CharT
*
s, size_type count,
const Allocator & alloc = Allocator ( ) ) ; |
(6) | (constexpr seit C++20) |
|
basic_string
(
const
CharT
*
s,
const
Allocator
&
alloc
=
Allocator
(
)
)
;
|
(7) | (constexpr seit C++20) |
|
basic_string
(
std::
nullptr_t
)
=
delete
;
|
(8) | (seit C++23) |
|
template
<
class
StringViewLike
>
explicit
basic_string
(
const
StringViewLike
&
t,
|
(9) |
(seit C++17)
(constexpr seit C++20) |
|
template
<
class
StringViewLike
>
basic_string
(
const
StringViewLike
&
t,
|
(10) | (seit C++17) |
|
basic_string
(
const
basic_string
&
other
)
;
|
(11) | (constexpr seit C++20) |
|
basic_string
(
basic_string
&&
other
)
noexcept
;
|
(12) |
(seit C++11)
(constexpr seit C++20) |
|
basic_string
(
const
basic_string
&
other,
const
Allocator
&
alloc
)
;
|
(13) |
(seit C++11)
(constexpr seit C++20) |
|
basic_string
(
basic_string
&&
other,
const
Allocator
&
alloc
)
;
|
(14) |
(seit C++11)
(constexpr seit C++20) |
|
basic_string
(
const
basic_string
&
other, size_type pos,
const Allocator & alloc = Allocator ( ) ) ; |
(15) | (constexpr seit C++20) |
|
constexpr
basic_string
(
basic_string
&&
other, size_type pos,
const Allocator & alloc = Allocator ( ) ) ; |
(16) | (seit C++23) |
|
basic_string
(
const
basic_string
&
other,
size_type pos, size_type count,
|
(17) | (constexpr seit C++20) |
|
constexpr
basic_string
(
basic_string
&&
other,
size_type pos, size_type count,
|
(18) | (seit C++23) |
|
basic_string
(
std::
initializer_list
<
CharT
>
ilist,
const Allocator & alloc = Allocator ( ) ) ; |
(19) |
(seit C++11)
(constexpr seit C++20) |
Konstruiert einen neuen String aus verschiedenen Datenquellen und optional unter Verwendung eines benutzerdefinierten Allokators alloc .
|
Falls
|
(since C++11) |
|
Diese Überladung nimmt nur dann an der Überladungsauflösung teil, wenn
|
(since C++17) |
[
first
,
last
)
. Jeder Iterator in
[
first
,
last
)
wird genau einmal dereferenziert.
|
Falls
|
(bis C++11) |
|
Diese Überladung nimmt nur an der Überladungsauflösung teil, falls
Falls
|
(seit C++11) |
CharT
nicht
EmplaceConstructible
in
std::
basic_string
<
CharT
>
von
*
ranges::
begin
(
rg
)
ist, ist das Verhalten undefiniert.
[
s
,
s
+
count
)
.
[
s
,
s
+
count
)
kein gültiger Bereich ist, ist das Verhalten undefiniert.
|
Diese Überladung nimmt nur dann an der Überladungsauflösung teil, wenn
|
(seit C++17) |
std::basic_string
kann nicht aus
nullptr
konstruiert werden.
std:: basic_string_view < CharT, Traits >> true ist und std:: is_convertible_v < const StringViewLike & , const CharT * > false ist.
std:: basic_string_view < CharT, Traits >> true ist.
basic_string&&
ist, befindet sich
other
nach Abschluss der Konstruktion in einem gültigen, aber unspezifizierten Zustand.
|
Der Allokator wird erhalten wie durch Aufruf von
std::
allocator_traits
<
Allocator
>
::
|
(since C++11) |
[
other.
data
(
)
+
pos
,
other.
data
(
)
+
other.
size
(
)
)
.
[
other.
data
(
)
+
pos
,
other.
data
(
)
+
(
pos
+
std::
min
(
count, other.
size
(
)
-
pos
)
)
)
.
Inhaltsverzeichnis |
Parameter
| alloc | - | Allokator, der für alle Speicherallokationen dieser Zeichenkette verwendet wird |
| count | - | Größe der resultierenden Zeichenkette |
| ch | - | Wert, mit dem die Zeichenkette initialisiert wird |
| pos | - | Position des ersten einzubeziehenden Zeichens |
| first, last | - | Bereich, aus dem die Zeichen kopiert werden |
| s | - | Zeiger auf ein Zeichenarray, das als Quelle zur Initialisierung der Zeichenkette dient |
| other | - | Eine weitere Zeichenkette, die als Quelle zur Initialisierung verwendet wird |
| ilist | - | std::initializer_list zur Initialisierung der Zeichenkette |
| t | - | Objekt (konvertierbar zu std::basic_string_view ) zur Initialisierung der Zeichenkette |
| rg | - | Ein Container-kompatibler Bereich |
Komplexität
Ausnahmen
Wirft
std::length_error
, wenn die Länge der konstruierten Zeichenkette
max_size()
überschreiten würde
(zum Beispiel, wenn
count
>
max_size
(
)
für
(3)
). Aufrufe von
Allocator::allocate
können Ausnahmen werfen.
Wenn aus irgendeinem Grund eine Exception ausgelöst wird, haben diese Funktionen keine Wirkung ( strong exception safety guarantee ).
Hinweise
Initialisierung mit einem
string literal
, das eingebettete
'
\0
'
-Zeichen enthält, verwendet die Überladung
(7)
, die beim ersten Nullzeichen stoppt. Dies kann vermieden werden, indem ein anderer Konstruktor angegeben oder
operator""s
verwendet wird:
std::string s1 = "ab\0\0cd"; // s1 enthält "ab" std::string s2{"ab\0\0cd", 6}; // s2 enthält "ab\0\0cd" std::string s3 = "ab\0\0cd"s; // s3 enthält "ab\0\0cd"
| Feature-Test Makro | Wert | Std | Feature |
|---|---|---|---|
__cpp_lib_containers_ranges
|
202202L
|
(C++23) | Getaggter Konstruktor (5) zur Konstruktion aus container-kompatiblem Bereich |
Beispiel
#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'; // nicht spezifiziert std::cout << "3) string(size_type count, CharT ch): "; std::string s2(4, '='); std::cout << std::quoted(s2) << '\n'; // "====" std::cout << "4) string(InputIt first, InputIt last): "; char mutable_c_str[] = "ein weiterer C-String"; std::string s4(std::begin(mutable_c_str) + 8, std::end(mutable_c_str) - 1); std::cout << std::quoted(s4) << '\n'; // "C-style string" std::cout << "6) string(CharT const* s, size_type count): "; std::string s6("C-style string", 7); std::cout << std::quoted(s6) << '\n'; // "C-style", d.h. [0, 7) std::cout << "7) string(CharT const* s): "; std::string s7("C-style\0string"); std::cout << std::quoted(s7) << '\n'; // "C-style" std::cout << "11) string(string&): "; std::string const other11("Exemplar"); std::string s11(other11); std::cout << std::quoted(s11) << '\n'; // "Exemplar" std::cout << "12) string(string&&): "; std::string s12(std::string("C++ von ") + std::string("Beispiel")); std::cout << std::quoted(s12) << '\n'; // "C++ by example" std::cout << "15) string(const string& other, size_type pos): "; std::string const other15("Mutatis Mutandis"); std::string s15(other15, 8); std::cout << std::quoted(s15) << '\n'; // "Mutandis", d.h. [8, 16) std::cout << "17) string(const string& other, size_type pos, size_type count): "; std::string const other17("Exemplarisch"); std::string s17(other17, 0, other17.length() - 1); std::cout << std::quoted(s17) << '\n'; // "Exemplar" std::cout << "19) string(std::initializer_list<CharT>): "; std::string s19({'C', '-', 's', 't', 'y', 'l', 'e'}); std::cout << std::quoted(s19) << '\n'; // "C-Style" }
Mögliche Ausgabe:
1) string(); s1.capacity(): 15 3) string(size_type count, CharT ch): "====" 4) string(InputIt first, InputIt last): "C-style string" 6) string(CharT const* s, size_type count): "C-style" 7) string(CharT const* s): "C-style" 11) string(string&): "Exemplar" 12) string(string&&): "C++ by example" 15) string(const string& other, size_type pos): "Mutandis" 17) string(const string& other, size_type pos, size_type count): "Exemplar" 19) string(std::initializer_list<CharT>): "C-style"
Fehlerberichte
Die folgenden verhaltensändernden Fehlerberichte wurden rückwirkend auf zuvor veröffentlichte C++-Standards angewendet.
| DR | Angewendet auf | Verhalten wie veröffentlicht | Korrigiertes Verhalten |
|---|---|---|---|
| LWG 301 | C++98 |
Überladung
(4)
verwendete den Parameter
alloc nicht, wenn
InputIt
ein integraler Typ ist
|
diesen Parameter verwenden |
| LWG 438 | C++98 |
Überladung
(4)
würde nur Überladung
(3)
aufrufen, wenn
InputIt
ein integraler Typ ist
|
ruft Überladung
(3)
auf, wenn
InputIt
kein LegacyInputIterator ist |
| LWG 847 | C++98 | es gab keine Ausnahmesicherheitsgarantie | starke Ausnahmesicherheitsgarantie hinzugefügt |
| LWG 2193 | C++11 | der Standardkonstruktor ist explizit | nicht-explizit gemacht |
| LWG 2235 | C++98 | s könnte ein Nullzeigerwert sein | das Verhalten ist in diesem Fall undefiniert |
| LWG 2250 | C++98 |
das Verhalten von Überladung
(17)
war undefiniert
wenn pos > other. size ( ) true ist |
wirft in diesem Fall immer
eine Ausnahme |
| LWG 2583 | C++98 | es gab keine Möglichkeit, einen Allokator für Überladung (17) bereitzustellen | Überladung (15) hinzugefügt |
| LWG 2946 | C++17 | Überladung (9) verursacht in einigen Fällen Mehrdeutigkeit | vermieden durch Template-Implementierung |
| LWG 3076 | C++17 |
Überladungen
(3,7)
könnten Mehrdeutigkeiten
bei der Klassentemplate-Argumentableitung verursachen |
eingeschränkt |
|
LWG 3111
( P1148R0 ) |
C++98 |
die Lösung von
LWG Issue 2235
machte das
Verhalten von basic_string ( nullptr, 0 ) undefiniert |
das Verhalten ist in diesem
Fall wohldefiniert [1] |
-
↑
[nullptr,nullptr + 0)ist ein gültiger leerer Bereich, da das Addieren von Null zu einem Nullzeigerwert ebenfalls wohldefiniert ist (das Ergebnis bleibt ein Nullzeigerwert).
Siehe auch
|
Zeichen einer Zeichenkette zuweisen
(öffentliche Elementfunktion) |
|
|
weist Werte der Zeichenkette zu
(öffentliche Elementfunktion) |
|
|
(C++11)
|
konvertiert einen ganzzahligen oder Gleitkommawert zu
string
(Funktion) |
|
(C++11)
|
konvertiert einen ganzzahligen oder Gleitkommawert zu
wstring
(Funktion) |
konstruiert einen
basic_string_view
(öffentliche Elementfunktion von
std::basic_string_view<CharT,Traits>
)
|