<string>
– operátory
operator!=
operator>
operator>>
operator>=
operator<
operator<<
operator<=
operator+
operator==
operator+
Zřetězí dva řetězcové objekty.
template <class CharType, class Traits, class Allocator>
basic_string<CharType, Traits, Allocator> operator+(
const basic_string<CharType, Traits, Allocator>& left,
const basic_string<CharType, Traits, Allocator>& right);
template <class CharType, class Traits, class Allocator>
basic_string<CharType, Traits, Allocator> operator+(
const basic_string<CharType, Traits, Allocator>& left,
const CharType* right);
template <class CharType, class Traits, class Allocator>
basic_string<CharType, Traits, Allocator> operator+(
const basic_string<CharType, Traits, Allocator>& left,
const CharType right);
template <class CharType, class Traits, class Allocator>
basic_string<CharType, Traits, Allocator> operator+(
const CharType* left,
const basic_string<CharType, Traits, Allocator>& right);
template <class CharType, class Traits, class Allocator>
basic_string<CharType, Traits, Allocator> operator+(
const CharType left,
const basic_string<CharType, Traits, Allocator>& right);
template <class CharType, class Traits, class Allocator>
basic_string<CharType, Traits, Allocator>&& operator+(
const basic_string<CharType, Traits, Allocator>& left,
const basic_string<CharType, Traits, Allocator>&& right);
template <class CharType, class Traits, class Allocator>
basic_string<CharType, Traits, Allocator>&& operator+(
const basic_string<CharType, Traits, Allocator>&& left,
const basic_string<CharType, Traits, Allocator>& right);
template <class CharType, class Traits, class Allocator>
basic_string<CharType, Traits, Allocator>&& operator+(
const basic_string<CharType, Traits, Allocator>&& left,
const basic_string<CharType, Traits, Allocator>&& right);
template <class CharType, class Traits, class Allocator>
basic_string<CharType, Traits, Allocator>&& operator+(
const basic_string<CharType, Traits, Allocator>&& left,
const CharType* right);
template <class CharType, class Traits, class Allocator>
basic_string<CharType, Traits, Allocator>&& operator+(
const basic_string<CharType, Traits, Allocator>&& left,
CharType right);
template <class CharType, class Traits, class Allocator>
basic_string<CharType, Traits, Allocator>&& operator+(
const CharType* left,
const basic_string<CharType, Traits, Allocator>&& right);
template <class CharType, class Traits, class Allocator>
basic_string<CharType, Traits, Allocator>&& operator+(
CharType left,
const basic_string<CharType, Traits, Allocator>&& right);
Parametry
Vlevo
Řetězec ve stylu jazyka C nebo objekt typu basic_string
, který má být zřetězen.
Vpravo
Řetězec ve stylu jazyka C nebo objekt typu basic_string
, který má být zřetězen.
Návratová hodnota
Řetězec, který je zřetězením vstupních řetězců.
Poznámky
Funkce každé přetížení operator+
zřetězení dvou objektů šablony třídy basic_string Class. Všechny efektivně vrátit basic_string< CharType, Traits, Allocator>(Left).append(right)
. Další informace najdete v tématu připojení.
Příklad
// string_op_con.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
{
using namespace std;
// Declaring an object of type basic_string<char>
string s1 ( "anti" );
string s2 ( "gravity" );
cout << "The basic_string s1 = " << s1 << "." << endl;
cout << "The basic_string s2 = " << s2 << "." << endl;
// Declaring a C-style string
char *s3 = "heroine";
cout << "The C-style string s3 = " << s3 << "." << endl;
// Declaring a character constant
char c1 = '!';
cout << "The character constant c1 = " << c1 << "." << endl;
// First member function: concatenates an object
// of type basic_string with an object of type basic_string
string s12 = s1 + s2;
cout << "The string concatenating s1 & s2 is: " << s12 << endl;
// Second & fourth member functions: concatenate an object
// of type basic_string with an object of C-syle string type
string s1s3 = s1 + s3;
cout << "The string concatenating s1 & s3 is: " << s1s3 << endl;
// Third & fifth member functions: concatenate an object
// of type basic_string with a character constant
string s1s3c1 = s1s3 + c1;
cout << "The string concatenating s1 & s3 is: " << s1s3c1 << endl;
}
The basic_string s1 = anti.
The basic_string s2 = gravity.
The C-style string s3 = heroine.
The character constant c1 = !.
The string concatenating s1 & s2 is: antigravity
The string concatenating s1 & s3 is: antiheroine
The string concatenating s1 & s3 is: antiheroine!
operator!=
Testuje, zda objekt řetězce na levé straně operátoru není roven objektu řetězce na pravé straně.
template <class CharType, class Traits, class Allocator>
bool operator!=(
const basic_string<CharType, Traits, Allocator>& left,
const basic_string<CharType, Traits, Allocator>& right);
template <class CharType, class Traits, class Allocator>
bool operator!=(
const basic_string<CharType, Traits, Allocator>& left,
const CharType* right);
template <class CharType, class Traits, class Allocator>
bool operator!=(
const CharType* left,
const basic_string<CharType, Traits, Allocator>& right);
Parametry
Vlevo
Řetězec ve stylu jazyka C nebo objekt typu basic_string
, který se má porovnat.
Vpravo
Řetězec ve stylu jazyka C nebo objekt typu basic_string
, který se má porovnat.
Návratová hodnota
true
pokud řetězcový objekt na levé straně operátoru není lexicicky roven řetězcového objektu na pravé straně; jinak false
.
Poznámky
Porovnání řetězcových objektů je založeno na párovém lexicografickém porovnání jejich znaků. Dva řetězce jsou stejné, pokud mají stejný počet znaků a jejich odpovídající hodnoty znaků jsou stejné. Jinak jsou nerovné.
Příklad
// string_op_ne.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
{
using namespace std;
// Declaring an objects of type basic_string<char>
string s1 ( "pluck" );
string s2 ( "strum" );
cout << "The basic_string s1 = " << s1 << "." << endl;
cout << "The basic_string s2 = " << s2 << "." << endl;
// Declaring a C-style string
char *s3 = "pluck";
cout << "The C-style string s3 = " << s3 << "." << endl;
// First member function: comparison between left-side object
// of type basic_string & right-side object of type basic_string
if ( s1 != s2 )
cout << "The strings s1 & s2 are not equal." << endl;
else
cout << "The strings s1 & s2 are equal." << endl;
// Second member function: comparison between left-side object
// of type basic_string & right-side object of C-syle string type
if ( s1 != s3 )
cout << "The strings s1 & s3 are not equal." << endl;
else
cout << "The strings s1 & s3 are equal." << endl;
// Third member function: comparison between left-side object
// of C-syle string type & right-side object of type basic_string
if ( s3 != s2 )
cout << "The strings s3 & s2 are not equal." << endl;
else
cout << "The strings s3 & s2 are equal." << endl;
}
The basic_string s1 = pluck.
The basic_string s2 = strum.
The C-style string s3 = pluck.
The strings s1 & s2 are not equal.
The strings s1 & s3 are equal.
The strings s3 & s2 are not equal.
operator==
Testuje, zda je objekt řetězce na levé straně operátoru roven řetězcového objektu na pravé straně.
template <class CharType, class Traits, class Allocator>
bool operator==(
const basic_string<CharType, Traits, Allocator>& left,
const basic_string<CharType, Traits, Allocator>& right);
template <class CharType, class Traits, class Allocator>
bool operator==(
const basic_string<CharType, Traits, Allocator>& left,
const CharType* right);
template <class CharType, class Traits, class Allocator>
bool operator==(
const CharType* left,
const basic_string<CharType, Traits, Allocator>& right);
Parametry
Vlevo
Řetězec ve stylu jazyka C nebo objekt typu basic_string
, který se má porovnat.
Vpravo
Řetězec ve stylu jazyka C nebo objekt typu basic_string
, který se má porovnat.
Návratová hodnota
true
je-li řetězcový objekt na levé straně operátoru lexicicky roven řetězcového objektu na pravé straně; jinak false
.
Poznámky
Porovnání řetězcových objektů je založeno na párovém lexicografickém porovnání jejich znaků. Dva řetězce jsou stejné, pokud mají stejný počet znaků a jejich odpovídající hodnoty znaků jsou stejné. Jinak jsou nerovné.
Příklad
// string_op_eq.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
{
using namespace std;
// Declaring an objects of type basic_string<char>
string s1 ( "pluck" );
string s2 ( "strum" );
cout << "The basic_string s1 = " << s1 << "." << endl;
cout << "The basic_string s2 = " << s2 << "." << endl;
// Declaring a C-style string
char *s3 = "pluck";
cout << "The C-style string s3 = " << s3 << "." << endl;
// First member function: comparison between left-side object
// of type basic_string & right-side object of type basic_string
if ( s1 == s2 )
cout << "The strings s1 & s2 are equal." << endl;
else
cout << "The strings s1 & s2 are not equal." << endl;
// Second member function: comparison between left-side object
// of type basic_string & right-side object of C-syle string type
if ( s1 == s3 )
cout << "The strings s1 & s3 are equal." << endl;
else
cout << "The strings s1 & s3 are not equal." << endl;
// Third member function: comparison between left-side object
// of C-syle string type & right-side object of type basic_string
if ( s3 == s2 )
cout << "The strings s3 & s2 are equal." << endl;
else
cout << "The strings s3 & s2 are not equal." << endl;
}
The basic_string s1 = pluck.
The basic_string s2 = strum.
The C-style string s3 = pluck.
The strings s1 & s2 are not equal.
The strings s1 & s3 are equal.
The strings s3 & s2 are not equal.
operator<
Testuje, zda objekt řetězce na levé straně operátoru je menší než řetězcový objekt na pravé straně.
template <class CharType, class Traits, class Allocator>
bool operator<(
const basic_string<CharType, Traits, Allocator>& left,
const basic_string<CharType, Traits, Allocator>& right);
template <class CharType, class Traits, class Allocator>
bool operator<(
const basic_string<CharType, Traits, Allocator>& left,
const CharType* right);
template <class CharType, class Traits, class Allocator>
bool operator<(
const CharType* left,
const basic_string<CharType, Traits, Allocator>& right);
Parametry
Vlevo
Řetězec ve stylu jazyka C nebo objekt typu basic_string
, který se má porovnat.
Vpravo
Řetězec ve stylu jazyka C nebo objekt typu basic_string
, který se má porovnat.
Návratová hodnota
true
je-li řetězcový objekt na levé straně operátoru lexicicky menší než řetězcový objekt na pravé straně; jinak false
.
Poznámky
Lexikografické porovnání mezi řetězci je porovnává znakem podle znaků, dokud:
Najde dva odpovídající znaky nerovné a výsledek jejich porovnání se bere jako výsledek porovnání mezi řetězci.
Nenajde žádné nerovnosti, ale jeden řetězec má více znaků než druhý a kratší řetězec se považuje za menší než delší řetězec.
Nenajde žádné nerovnosti a zjistí, že řetězce mají stejný počet znaků, a proto jsou řetězce stejné.
Příklad
// string_op_lt.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
{
using namespace std;
// Declaring an objects of type basic_string<char>
string s1 ( "strict" );
string s2 ( "strum" );
cout << "The basic_string s1 = " << s1 << "." << endl;
cout << "The basic_string s2 = " << s2 << "." << endl;
// Declaring a C-style string
char *s3 = "strict";
cout << "The C-style string s3 = " << s3 << "." << endl;
// First member function: comparison between left-side object
// of type basic_string & right-side object of type basic_string
if ( s1 < s2 )
cout << "The string s1 is less than the string s2." << endl;
else
cout << "The string s1 is not less than the string s2." << endl;
// Second member function: comparison between left-hand object
// of type basic_string & right-hand object of C-syle string type
if ( s1 < s3 )
cout << "The string s1 is less than the string s3." << endl;
else
cout << "The string s1 is not less than the string s3." << endl;
// Third member function: comparison between left-hand object
// of C-syle string type & right-hand object of type basic_string
if ( s3 < s2 )
cout << "The string s3 is less than the string s2." << endl;
else
cout << "The string s3 is not less than the string s2." << endl;
}
The basic_string s1 = strict.
The basic_string s2 = strum.
The C-style string s3 = strict.
The string s1 is less than the string s2.
The string s1 is not less than the string s3.
The string s3 is less than the string s2.
operator<=
Testuje, zda řetězcový objekt na levé straně operátoru je menší nebo roven objektu řetězce na pravé straně.
template <class CharType, class Traits, class Allocator>
bool operator<=(
const basic_string<CharType, Traits, Allocator>& left,
const basic_string<CharType, Traits, Allocator>& right);
template <class CharType, class Traits, class Allocator>
bool operator<=(
const basic_string<CharType, Traits, Allocator>& left,
const CharType* right);
template <class CharType, class Traits, class Allocator>
bool operator<=(
const CharType* left,
const basic_string<CharType, Traits, Allocator>& right);
Parametry
Vlevo
Řetězec ve stylu jazyka C nebo objekt typu basic_string
, který se má porovnat.
Vpravo
Řetězec ve stylu jazyka C nebo objekt typu basic_string
, který se má porovnat.
Návratová hodnota
true
je-li řetězcový objekt na levé straně operátoru lexicicky menší nebo roven řetězcového objektu na pravé straně; jinak false
.
Poznámky
Lexikografické porovnání mezi řetězci je porovnává znakem podle znaků, dokud:
Najde dva odpovídající znaky nerovné a výsledek jejich porovnání se bere jako výsledek porovnání mezi řetězci.
Nenajde žádné nerovnosti, ale jeden řetězec má více znaků než druhý a kratší řetězec se považuje za menší než delší řetězec.
Nenajde žádné nerovnosti a zjistí, že řetězce mají stejný počet znaků, takže řetězce jsou stejné.
Příklad
// string_op_le.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
{
using namespace std;
// Declaring an objects of type basic_string<char>
string s1 ( "strict" );
string s2 ( "strum" );
cout << "The basic_string s1 = " << s1 << "." << endl;
cout << "The basic_string s2 = " << s2 << "." << endl;
// Declaring a C-style string
char *s3 = "strict";
cout << "The C-style string s3 = " << s3 << "." << endl;
// First member function: comparison between left-side object
// of type basic_string & right-side object of type basic_string
if ( s1 <= s2 )
cout << "The string s1 is less than or equal to "
<< "the string s2." << endl;
else
cout << "The string s1 is greater than "
<< "the string s2." << endl;
// Second member function: comparison between left-side object
// of type basic_string & right-side object of C-syle string type
if ( s1 <= s3 )
cout << "The string s1 is less than or equal to "
<< "the string s3." << endl;
else
cout << "The string s1 is greater than "
<< "the string s3." << endl;
// Third member function: comparison between left-side object
// of C-syle string type & right-side object of type basic_string
if ( s2 <= s3 )
cout << "The string s2 is less than or equal to "
<< "the string s3." << endl;
else
cout << "The string s2 is greater than "
<< "the string s3." << endl;
}
The basic_string s1 = strict.
The basic_string s2 = strum.
The C-style string s3 = strict.
The string s1 is less than or equal to the string s2.
The string s1 is less than or equal to the string s3.
The string s2 is greater than the string s3.
operator<<
Funkce šablony, která zapíše řetězec do výstupního datového proudu.
template <class CharType, class Traits, class Allocator>
basic_ostream<CharType, Traits>& operator<<(
basic_ostream<CharType, Traits>& _Ostr,
const basic_string<CharType, Traits, Allocator>& str);
Parametry
_Ostr
Výstupní datový proud, do kterého se zapisuje.
Str
Řetězec, který se má zadat do výstupního datového proudu.
Návratová hodnota
Zapíše hodnotu zadaného řetězce do výstupního datového proudu _Ostr.
Poznámky
Funkce šablony přetíží operátor<< vložení objektu str šablony třídy basic_string do datového proudu _Ostr. Funkce efektivně vrátí _Ostr.write( str.c_str, str.size )
.
operator>
Testuje, jestli je objekt řetězce na levé straně operátoru větší než objekt řetězce na pravé straně.
template <class CharType, class Traits, class Allocator>
bool operator>(
const basic_string<CharType, Traits, Allocator>& left,
const basic_string<CharType, Traits, Allocator>& right);
template <class CharType, class Traits, class Allocator>
bool operator>(
const basic_string<CharType, Traits, Allocator>& left,
const CharType* right);
template <class CharType, class Traits, class Allocator>
bool operator>(
const CharType* left,
const basic_string<CharType, Traits, Allocator>& right);
Parametry
Vlevo
Řetězec ve stylu jazyka C nebo objekt typu basic_string
, který se má porovnat.
Vpravo
Řetězec ve stylu jazyka C nebo objekt typu basic_string
, který se má porovnat.
Návratová hodnota
true
je-li řetězcový objekt na levé straně operátoru lexicicky větší než řetězcový objekt na pravé straně; jinak false
.
Poznámky
Lexikografické porovnání mezi řetězci je porovnává znakem podle znaků, dokud:
Najde dva odpovídající znaky nerovné a výsledek jejich porovnání se bere jako výsledek porovnání mezi řetězci.
Nenajde žádné nerovnosti, ale jeden řetězec má více znaků než druhý a kratší řetězec se považuje za menší než delší řetězec.
Nenajde žádné nerovnosti a zjistí, že řetězce mají stejný počet znaků, a proto jsou řetězce stejné.
Příklad
// string_op_gt.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
{
using namespace std;
// Declaring an objects of type basic_string<char>
string s1 ( "strict" );
string s2 ( "strum" );
cout << "The basic_string s1 = " << s1 << "." << endl;
cout << "The basic_string s2 = " << s2 << "." << endl;
// Declaring a C-style string
char *s3 = "stricture";
cout << "The C-style string s3 = " << s3 << "." << endl;
// First member function: comparison between left-side object
// of type basic_string & right-side object of type basic_string
if ( s1 > s2 )
cout << "The string s1 is greater than "
<< "the string s2." << endl;
else
cout << "The string s1 is not greater than "
<< "the string s2." << endl;
// Second member function: comparison between left-side object
// of type basic_string & right-side object of C-syle string type
if ( s3 > s1 )
cout << "The string s3 is greater than "
<< "the string s1." << endl;
else
cout << "The string s3 is not greater than "
<< "the string s1." << endl;
// Third member function: comparison between left-side object
// of C-syle string type & right-side object of type basic_string
if ( s2 > s3 )
cout << "The string s2 is greater than "
<< "the string s3." << endl;
else
cout << "The string s2 is not greater than "
<< "the string s3." << endl;
}
The basic_string s1 = strict.
The basic_string s2 = strum.
The C-style string s3 = stricture.
The string s1 is not greater than the string s2.
The string s3 is greater than the string s1.
The string s2 is greater than the string s3.
operator>=
Testuje, zda řetězcový objekt na levé straně operátoru je větší nebo roven objektu řetězce na pravé straně.
template <class CharType, class Traits, class Allocator>
bool operator>=(
const basic_string<CharType, Traits, Allocator>& left,
const basic_string<CharType, Traits, Allocator>& right);
template <class CharType, class Traits, class Allocator>
bool operator>=(
const basic_string<CharType, Traits, Allocator>& left,
const CharType* right);
template <class CharType, class Traits, class Allocator>
bool operator>=(
const CharType* left,
const basic_string<CharType, Traits, Allocator>& right);
Parametry
Vlevo
Řetězec ve stylu jazyka C nebo objekt typu basic_string
, který se má porovnat.
Vpravo
Řetězec ve stylu jazyka C nebo objekt typu basic_string
, který se má porovnat.
Návratová hodnota
true
je-li řetězcový objekt na levé straně operátoru lexicicky větší nebo roven řetězcového objektu na pravé straně; jinak false
.
Poznámky
Lexikografické porovnání mezi řetězci je porovnává znakem podle znaků, dokud:
Najde dva odpovídající znaky nerovné a výsledek jejich porovnání se bere jako výsledek porovnání mezi řetězci.
Nenajde žádné nerovnosti, ale jeden řetězec má více znaků než druhý a kratší řetězec se považuje za menší než delší řetězec.
Nenajde žádné nerovnosti a najde řetězce se stejným počtem znaků, takže řetězce jsou stejné.
Příklad
// string_op_ge.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
{
using namespace std;
// Declaring an objects of type basic_string<char>
string s1 ( "strict" );
string s2 ( "strum" );
cout << "The basic_string s1 = " << s1 << "." << endl;
cout << "The basic_string s2 = " << s2 << "." << endl;
// Declaring a C-style string
char *s3 = "stricture";
cout << "The C-style string s3 = " << s3 << "." << endl;
// First member function: comparison between left-side object
// of type basic_string & right-side object of type basic_string
if ( s1 >= s2 )
cout << "The string s1 is greater than or equal to "
<< "the string s2." << endl;
else
cout << "The string s1 is less than "
<< "the string s2." << endl;
// Second member function: comparison between left-side object
// of type basic_string & right-side object of C-syle string type
if ( s3 >= s1 )
cout << "The string s3 is greater than or equal to "
<< "the string s1." << endl;
else
cout << "The string s3 is less than "
<< "the string s1." << endl;
// Third member function: comparison between left-side object
// of C-syle string type & right-side object of type basic_string
if ( s2 >= s3 )
cout << "The string s2 is greater than or equal to "
<< "the string s3." << endl;
else
cout << "The string s2 is less than "
<< "the string s3." << endl;
}
The basic_string s1 = strict.
The basic_string s2 = strum.
The C-style string s3 = stricture.
The string s1 is less than the string s2.
The string s3 is greater than or equal to the string s1.
The string s2 is greater than or equal to the string s3.
operator>>
Funkce šablony, která čte řetězec ze vstupního datového proudu.
template <class CharType, class Traits, class Allocator>
basic_istream<CharType, Traits>& operator>>(
basic_istream<CharType, Traits>& _Istr,
basic_string<CharType, Traits, Allocator>& right);
Parametry
_Istr
Vstupní datový proud použitý k extrakci sekvence
Vpravo
Řetězec, který se extrahuje ze vstupního datového proudu.
Návratová hodnota
Načte hodnotu zadaného řetězce z _Istr a vrátí ji doprava.
Poznámky
Operátor přeskočí úvodní prázdné znaky, pokud skipws
není nastavený příznak. Přečte všechny následující znaky, dokud další znak nebude prázdný nebo se nedosáhne konce souboru.
Funkce šablony přetěžuje operátor>> , který nahradí sekvenci řízenou vpravo posloupností prvků extrahovaných z datového proudu _Istr. Zarážky extrakce:
Na konci souboru.
Za funkcí extrahuje
_Istr
. prvky šířky , pokud je tato hodnota nenulová.
Za funkcí extrahuje _Istr
. max_size elementy.
- Za funkcí extrahuje prvek ch, pro který use_facet<ctype<CharType>>( ).
getloc
is( ctype<CharType>:: space, ch) je true, v takovém případě se znak vrátí zpět.
Pokud funkce extrahuje žádné prvky, volá setstate(ios_base::failbit
). V každém případě volá istr. width(0) a vrátí * this
.
Příklad
// string_op_read_.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
{
using namespace std;
string c0;
cout << "Input a string c0 ( try: Fibonacci numbers ): ";
cin >> c0;
cout << "The string entered is c0 = " << c0 << endl;
}