Namespaces
Variants

std::basic_string<CharT,Traits,Allocator>:: basic_string

From cppreference.net
std::basic_string
(Anmerkung: Der bereitgestellte HTML-Code enthält keinen übersetzbaren Text, da alle Tags leer sind. Die Struktur bleibt gemäß den Anforderungen unverändert.)
(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,

const Allocator & alloc = Allocator ( ) ) ;
(4) (constexpr seit C++20)
template < container-compatible-range < CharT > R >

constexpr basic_string ( std:: from_range_t , R && rg,

const Allocator & = Allocator ( ) ) ;
(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,

const Allocator & alloc = Allocator ( ) ) ;
(9) (seit C++17)
(constexpr seit C++20)
template < class StringViewLike >

basic_string ( const StringViewLike & t,
size_type pos, size_type count,

const Allocator & alloc = Allocator ( ) ) ;
(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,

const Allocator & alloc = Allocator ( ) ) ;
(17) (constexpr seit C++20)
constexpr basic_string ( basic_string && other,

size_type pos, size_type count,

const Allocator & alloc = Allocator ( ) ) ;
(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 .

1) Der Standardkonstruktor seit C++11. Konstruiert einen leeren String mit einem standardkonstruierten Allokator.
Falls Allocator nicht DefaultConstructible ist, ist das Verhalten undefiniert.
2) Der Standardkonstruktor bis C++11. Konstruiert einen leeren String mit dem angegebenen Allokator alloc .
3) Konstruiert einen String mit count Kopien des Zeichens ch .

Falls CharT nicht CopyInsertable in std:: basic_string < CharT > ist, ist das Verhalten undefiniert.

(since C++11)

Diese Überladung nimmt nur dann an der Überladungsauflösung teil, wenn Allocator die Anforderungen von Allocator erfüllt.

(since C++17)
4) Konstruiert einen String mit den Inhalten des Bereichs [ first , last ) . Jeder Iterator in [ first , last ) wird genau einmal dereferenziert.

Falls InputIt die Anforderungen von LegacyInputIterator nicht erfüllt, wird Überladung (3) stattdessen mit den Argumenten static_cast < size_type > ( first ) , last und alloc aufgerufen.

(bis C++11)

Diese Überladung nimmt nur an der Überladungsauflösung teil, falls InputIt die Anforderungen von LegacyInputIterator erfüllt.

Falls CharT nicht EmplaceConstructible in std:: basic_string < CharT > von * first ist, ist das Verhalten undefiniert.

(seit C++11)
5) Konstruiert einen String mit den Inhalten des Bereichs rg . Jeder Iterator in rg wird genau einmal dereferenziert.
Wenn CharT nicht EmplaceConstructible in std:: basic_string < CharT > von * ranges:: begin ( rg ) ist, ist das Verhalten undefiniert.
6) Konstruiert einen String mit den Inhalten des Bereichs [ s , s + count ) .
Wenn [ s , s + count ) kein gültiger Bereich ist, ist das Verhalten undefiniert.
7) Entspricht basic_string ( s, Traits :: length ( s ) , alloc ) .

Diese Überladung nimmt nur dann an der Überladungsauflösung teil, wenn Allocator die Anforderungen von Allocator erfüllt.

(seit C++17)
8) std::basic_string kann nicht aus nullptr konstruiert werden.
9) Konvertiert implizit t zu einem String-View sv als ob durch std:: basic_string_view < CharT, Traits > sv = t ; , dann konstruiert einen String als ob durch basic_string ( sv. data ( ) , sv. size ( ) , alloc ) .
Diese Überladung nimmt nur dann an der Überladungsauflösung teil, wenn std:: is_convertible_v < const StringViewLike & ,
std:: basic_string_view < CharT, Traits >>
true ist und std:: is_convertible_v < const StringViewLike & , const CharT * > false ist.
10) Konvertiert implizit t zu einem String-View sv als ob durch std:: basic_string_view < CharT, Traits > sv = t ; , dann konstruiert einen String als ob durch basic_string ( sv. substr ( pos, n ) , alloc ) .
Diese Überladung nimmt nur dann an der Überladungsauflösung teil, wenn std:: is_convertible_v < const StringViewLike & ,
std:: basic_string_view < CharT, Traits >>
true ist.
11-18) Konstruiert einen String mit (Teilen) des Inhalts von other . Wenn der Typ von other basic_string&& ist, befindet sich other nach Abschluss der Konstruktion in einem gültigen, aber unspezifizierten Zustand.
11) Der Kopierkonstruktor.

Der Allokator wird erhalten wie durch Aufruf von std:: allocator_traits < Allocator > ::
select_on_container_copy_construction
( other. get_allocator ( ) )
.

(since C++11)
12) Der Move-Konstruktor. Der Allokator wird durch Move-Konstruktion von other. get_allocator ( ) erhalten.
13) Gleich wie der Kopierkonstruktor, außer dass alloc als Allokator verwendet wird.
Wenn CharT nicht CopyInsertable in std:: basic_string < CharT > ist, ist das Verhalten undefiniert.
14) Dasselbe wie der Move-Konstruktor, außer dass alloc als Allokator verwendet wird.
Wenn CharT nicht MoveInsertable in std:: basic_string < CharT > ist, ist das Verhalten undefiniert.
15,16) Konstruiert einen String mit dem Inhalt des Bereichs [ other. data ( ) + pos , other. data ( ) + other. size ( ) ) .
17,18) Konstruiert einen String mit dem Inhalt des Bereichs [ other. data ( ) + pos , other. data ( ) + ( pos + std:: min ( count, other. size ( ) - pos ) ) ) .
19) Entspricht basic_string ( ilist. begin ( ) , ilist. end ( ) ) .

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

1,2) Konstant.
3-7) Linear in der Größe der Zeichenkette.
9-11) Linear in der Größe der Zeichenkette.
12) Konstante.
13) Linear in der Größe der Zeichenkette.
14) Linear in der Größe der Zeichenkette, falls alloc ! = other. get_allocator ( ) true ist, andernfalls konstant.
15-19) Linear in der Größe der Zeichenkette.

Ausnahmen

10) std::out_of_range falls pos außerhalb des gültigen Bereichs liegt.
14) Wirft nichts, falls alloc == str. get_allocator ( ) true ist.
15-18) std::out_of_range wenn pos > other. size ( ) ist true .

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]
  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> )