Konstruktory stringów
Z cppreference.com
Składnia:
#include <string>
string();
string( const string& s );
string( size_type length, const char& ch );
string( const char* str );
string( const char* str, size_type length );
string( const string& str, size_type index, size_type length );
string( input_iterator start, input_iterator end );
~string();
Konstruktory te pozwalają na tworzenie stringów zawierających:
- nic; pusty string,
- kopię podanego stringa s,
- length kopii znaku ch,
- duplikat tablicy znaków str (opcjonalnie do długości length znaków),
- fragment stringa str od pozycji index i długości length znaków,
- string złożony ze znaków znajdujących się pomiędzy iteratorami start i end
Przykład:
string str1( 5, 'c' );
string str2( "Niezly hardkor!" );
string str3( str2, 7, 8 );
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
spowoduje wyświetlenie:
ccccc
Niezly hardkor!
hardkor!
Konstruktory stringów przeważnie działają w liniowym czasie, oprócz pustego konstruktora, który działa w stałym czasie.