Jegyzet
Az oldalhoz való hozzáférés engedélyezést igényel. Próbálhatod be jelentkezni vagy könyvtárat váltani.
Az oldalhoz való hozzáférés engedélyezést igényel. Megpróbálhatod a könyvtár váltását.
A <string> fejléc a következő operátorokat biztosítja:
operátor+
Összefűz két sztringobjektumot.
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);
Paraméterek
balra
Összefűzendő C-stílusú sztring vagy objektum basic_string .
jobb
Összefűzendő C-stílusú sztring vagy objektum basic_string .
Visszaadott érték
A bemeneti sztringek összefűzésének sztringje.
Megjegyzések
Az egyes túlterhelések operator+ függvényei az osztálysablon basic_stringkét objektumának összefűzéséhez. Minden hatékonyan vissza basic_string< CharType, Traits, Allocator>(Left).append(right). További információ: hozzáfűzés.
példa
// 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!
operátor!=
Ellenőrzi, hogy az operátor bal oldalán lévő sztringobjektum nem egyenlő-e a jobb oldalon lévő sztringobjektummal.
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);
Paraméterek
balra
Egy összehasonlítandó C-stílusú sztring vagy objektum basic_string .
jobb
Egy összehasonlítandó C-stílusú sztring vagy objektum basic_string .
Visszaadott érték
true ha az operátor bal oldalán lévő sztringobjektum nem lexikálisan egyenlő a jobb oldalon lévő sztringobjektummal; egyéb esetben false.
Megjegyzések
A sztringobjektumok összehasonlítása a karakterek egymáshoz viszonyított lexikográfiai összehasonlításán alapul. Két sztring egyenlő, ha azonos számú karakterrel rendelkeznek, és a hozzájuk tartozó karakterértékek megegyeznek. Ellenkező esetben egyenlőtlenek.
példa
// 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.
operátor==
Ellenőrzi, hogy az operátor bal oldalán lévő sztringobjektum megegyezik-e a jobb oldalon található sztringobjektummal.
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);
Paraméterek
balra
Egy összehasonlítandó C-stílusú sztring vagy objektum basic_string .
jobb
Egy összehasonlítandó C-stílusú sztring vagy objektum basic_string .
Visszaadott érték
true ha az operátor bal oldalán lévő sztringobjektum lexikálisan megegyezik a jobb oldalon lévő sztringobjektummal; egyéb esetben false.
Megjegyzések
A sztringobjektumok összehasonlítása a karakterek egymáshoz viszonyított lexikográfiai összehasonlításán alapul. Két sztring egyenlő, ha azonos számú karakterrel rendelkeznek, és a hozzájuk tartozó karakterértékek megegyeznek. Ellenkező esetben egyenlőtlenek.
példa
// 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<
Ellenőrzi, hogy az operátor bal oldalán lévő sztringobjektum kisebb-e, mint a jobb oldalon lévő sztringobjektum.
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);
Paraméterek
balra
Egy összehasonlítandó C-stílusú sztring vagy objektum basic_string .
jobb
Egy összehasonlítandó C-stílusú sztring vagy objektum basic_string .
Visszaadott érték
true ha az operátor bal oldalán lévő sztringobjektum lexikálisan kisebb, mint a jobb oldalon lévő sztringobjektum; egyéb esetben false.
Megjegyzések
A sztringek lexikográfiai összehasonlítása karakter szerint hasonlítja össze őket, amíg:
A függvény két megfelelő karaktert egyenlőtlennek talál, és az összehasonlítás eredménye a sztringek összehasonlítása lesz.
Nem talál egyenlőtlenségeket, de az egyik sztring több karaktert tartalmaz, mint a másik, és a rövidebb sztring kisebb, mint a hosszabb sztring.
Nem talál egyenlőtlenségeket, és megállapítja, hogy a sztringek száma azonos, így a sztringek egyenlőek.
példa
// 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<=
Ellenőrzi, hogy az operátor bal oldalán lévő sztringobjektum kisebb vagy egyenlő-e a jobb oldalon lévő sztringobjektummal.
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);
Paraméterek
balra
Egy összehasonlítandó C-stílusú sztring vagy objektum basic_string .
jobb
Egy összehasonlítandó C-stílusú sztring vagy objektum basic_string .
Visszaadott érték
true ha az operátor bal oldalán lévő sztringobjektum lexikálisan kisebb vagy egyenlő a jobb oldalon lévő sztringobjektummal; egyéb esetben false.
Megjegyzések
A sztringek lexikográfiai összehasonlítása karakter szerint hasonlítja össze őket, amíg:
A függvény két megfelelő karaktert egyenlőtlennek talál, és az összehasonlítás eredménye a sztringek összehasonlítása lesz.
Nem talál egyenlőtlenségeket, de az egyik sztring több karaktert tartalmaz, mint a másik, és a rövidebb sztring kisebb, mint a hosszabb sztring.
Nem talál egyenlőtlenségek, és megállapítja, hogy a sztringek száma azonos, így a sztringek egyenlőek.
példa
// 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<<
Egy sablonfüggvény, amely sztringet ír a kimeneti streambe.
template <class CharType, class Traits, class Allocator>
basic_ostream<CharType, Traits>& operator<<(
basic_ostream<CharType, Traits>& _Ostr,
const basic_string<CharType, Traits, Allocator>& str);
Paraméterek
_Ostr
A kimeneti stream, amelybe íródik.
Str
A kimeneti adatfolyamba beírandó sztring.
Visszaadott érték
A megadott sztring értékét írja a kimeneti streambe _Ostr.
Megjegyzések
A sablonfüggvény túlterheli az operátort<< , hogy beszúrjon egy objektum strosztálysablont basic_string a stream _Ostr. A függvény hatékonyan visszaadja a függvényt _Ostr.write( str.c_str, str.size ).
operator>
Ellenőrzi, hogy az operátor bal oldalán lévő sztringobjektum nagyobb-e, mint a jobb oldalon lévő sztringobjektum.
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);
Paraméterek
balra
Egy összehasonlítandó C-stílusú sztring vagy objektum basic_string .
jobb
Egy összehasonlítandó C-stílusú sztring vagy objektum basic_string .
Visszaadott érték
true ha az operátor bal oldalán lévő sztringobjektum lexikálisan nagyobb, mint a jobb oldalon lévő sztringobjektum; egyéb esetben false.
Megjegyzések
A sztringek lexikográfiai összehasonlítása karakter szerint hasonlítja össze őket, amíg:
A függvény két megfelelő karaktert egyenlőtlennek talál, és az összehasonlítás eredménye a sztringek összehasonlítása lesz.
Nem talál egyenlőtlenségeket, de az egyik sztring több karaktert tartalmaz, mint a másik, és a rövidebb sztring kisebb, mint a hosszabb sztring.
Nem talál egyenlőtlenségeket, és megállapítja, hogy a sztringek száma azonos, így a sztringek egyenlőek.
példa
// 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>=
Ellenőrzi, hogy az operátor bal oldalán lévő sztringobjektum nagyobb-e, mint a jobb oldalon lévő sztringobjektum.
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);
Paraméterek
balra
Egy összehasonlítandó C-stílusú sztring vagy objektum basic_string .
jobb
Egy összehasonlítandó C-stílusú sztring vagy objektum basic_string .
Visszaadott érték
true ha az operátor bal oldalán lévő sztringobjektum lexikálisan nagyobb vagy egyenlő a jobb oldalon lévő sztringobjektumnál; egyéb esetben false.
Megjegyzések
A sztringek lexikográfiai összehasonlítása karakter szerint hasonlítja össze őket, amíg:
A függvény két megfelelő karaktert egyenlőtlennek talál, és az összehasonlítás eredménye a sztringek összehasonlítása lesz.
Nem talál egyenlőtlenségeket, de az egyik sztring több karaktert tartalmaz, mint a másik, és a rövidebb sztring kisebb, mint a hosszabb sztring.
Nem talál egyenlőtlenségeket, és megállapítja, hogy a sztringek száma azonos, így a sztringek egyenlőek.
példa
// 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>>
Egy sablonfüggvény, amely egy bemeneti streamből olvas be egy sztringet.
template <class CharType, class Traits, class Allocator>
basic_istream<CharType, Traits>& operator>>(
basic_istream<CharType, Traits>& _Istr,
basic_string<CharType, Traits, Allocator>& right);
Paraméterek
_Istr
A sorozat kinyeréséhez használt bemeneti adatfolyam
jobb
A bemeneti adatfolyamból kinyert sztring.
Visszaadott érték
Beolvassa a megadott sztring értékét a _Istr , és visszaadja jobbra.
Megjegyzések
Az operátor kihagyja a kezdő szóközöket, kivéve, ha a skipws jelölő be van állítva. Az összes következő karaktert felolvassa, amíg a következő karakter üres hely vagy a fájl vége el nem éri.
A sablonfüggvény túlterheli az operátort>> , hogy lecserélje a jobb oldali vezérlésű sorozatot a streamből kinyert elemek sorozatára _Istr. Kinyerési leállások:
A fájl végén.
A függvény kinyerése
_Istrután. szélességi elemeket, ha ez az érték nem nulla.
A függvény kinyerése _Istrután.
max_size elemeket.
- Miután a függvény kinyer egy ch elemet, amelyhez use_facet< karaktertípus()<>>
getloc. is( ctype<CharType>:: space, ch) igaz, ebben az esetben a karakter vissza lesz helyezve.
Ha a függvény nem nyer ki elemeket, meghívja a setstate()ios_base::failbit függvényt. Mindenesetre a hívások istr.
szélesség(0) és * thisértéket ad vissza.
példa
// 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;
}