Operatory stringów
Składnia:
#include <string>
bool operator==(const string& c1, const string& c2);
bool operator!=(const string& c1, const string& c2);
bool operator<(const string& c1, const string& c2);
bool operator>(const string& c1, const string& c2);
bool operator<=(const string& c1, const string& c2);
bool operator>=(const string& c1, const string& c2);
string operator+(const string& s1, const string& s2 );
string operator+(const char* s, const string& s2 );
string operator+( char c, const string& s2 );
string operator+( const string& s1, const char* s );
string operator+( const string& s1, char c );
basic_string& operator+=(const basic_string& append);
basic_string& operator+=(const char* append);
basic_string& operator+=(const char append);
ostream& operator<<( ostream& os, const string& s );
istream& operator>>( istream& is, string& s );
string& operator=( const string& s );
string& operator=( const char* s );
string& operator=( char ch );
char& operator[]( size_type index );
Stringi C++ mogą być porównywane i przypisywane za pomocą standardowych operatorów porównania:
- ==**, **!=**, **<=**, **>=**, **<**, **>** oraz **=**.
Porównywanie lub przypisywanie stringów ma liniową złożoność czasową.
Dwa stringi są równe, jeśli:
- Ich rozmiar jest taki sam oraz
- Każdy element na pozycji i w pierwszym stringu jest równy elementowi na pozycji i w drugim stringu.
Porównywanie stringów przebiega leksykograficznie.
Poza normalnymi operatorami do pracy z tym pojemnikiem, stringi mogą być także łączone za pomocą operatora **+** oraz wstawiane do Strumieni I/O C++ przy użyciu operatorów **<<** oraz **>>**.
Przykład: następujący fragment kodu łączy dwa stringi i wyświetla rezultat:
string s1 = "Niezly ";
string s2 = "hardkor";
string s3 = s1 + s2;
cout << "s3 is " << s3 << endl;
Co więcej, stringom można przypisywać wartości innych stringów, tablice znaków lub też pojedyncze znaki. Poniższy kod jest całkowicie poprawny:
char ch = 'N';
string s;
s = ch;
Do pojedynczych znaków w stringu można się odnosić za pomocą operatora **[]**, który działa w stałym czasie.