<string>運算子</string>
如需 Visual Studio 2017 的最新文件請參閱 Visual Studio 2017 文件。
運算子 ! = | 運算子> | 運算子>> |
運算子>= | 運算子< | 運算子<< |
運算子<= | 運算子 + | 運算子 = = |
運算子 +
串連兩個字串物件。
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);
參數
left
要串連的 C 樣式字串或 basic_string
類型物件。
right
要串連的 C 樣式字串或 basic_string
類型物件。
傳回值
輸入字串的串連字串。
備註
每個函數多載operator+
來串連兩個物件的樣板類別basic_string 類別。 傳回所有有效basic_string
< CharType,特性,配置器> (_左)。 append(_ Right).
範例
// 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!
運算子 ! =
測試運算子左邊的字串物件是否不等於右邊的字串物件。
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);
參數
left
C-style 字串或物件型別的basic_string
進行比較。
right
C-style 字串或物件型別的basic_string
進行比較。
傳回值
true如果運算子左邊的字串物件不是進行字彙上等於右邊的字串物件,否則false。
備註
字串物件之間的比較為基礎的成對其字元的字彙比較。 兩個字串相等,如果有相同數目的字元,且其各自的字元值都是一樣。 反之則為不相等。
範例
// 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.
運算子 = =
測試運算子左邊的字串物件是否等於右邊的字串物件。
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);
參數
left
C-style 字串或物件型別的basic_string
進行比較。
right
C-style 字串或物件型別的basic_string
進行比較。
傳回值
true如果運算子左邊的字串物件進行字彙上等於右邊的字串物件,否則false。
備註
字串物件之間的比較為基礎的成對其字元的字彙比較。 兩個字串相等,如果有相同數目的字元,且其各自的字元值都是一樣。 反之則為不相等。
範例
// 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.
運算子<
測試運算子左邊的字串物件是否小於右邊的字串物件。
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);
參數
left
C-style 字串或物件型別的basic_string
進行比較。
right
C-style 字串或物件型別的basic_string
進行比較。
傳回值
true如果運算子左邊的字串物件是字數小於字串物件,在右側,否則false。
備註
字串的字彙比較比較它們,直到逐字元︰
找到兩個對應的字元不相等,並比較其結果會被視為字串之間比較的結果。
它會尋找任何不等式,但一個字串有字元數超過一個,以及較短的字串會被視為小於較長的字串。
它會尋找任何不等式會發現,此連接字串具有相同數目的字元,因此是相等的字串。
範例
// 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.
運算子<=
測試運算子左邊的字串物件是否小於或等於右邊的字串物件。
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);
參數
left
C-style 字串或物件型別的basic_string
進行比較。
right
C-style 字串或物件型別的basic_string
進行比較。
傳回值
true如果運算子左邊的字串物件進行字彙上小於或等於右邊的字串物件,; 否則false。
備註
字串的字彙比較比較它們,直到逐字元︰
找到兩個對應的字元不相等,並比較其結果會被視為字串之間比較的結果。
它會尋找任何不等式,但一個字串有字元數超過一個,以及較短的字串會被視為小於較長的字串。
它會尋找任何不等式會發現,字串有相同數目的字元,因此字串相等。
範例
// 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.
運算子<<
範本函式會將字串寫入到輸出資料流。
template <class CharType, class Traits, class Allocator>
basic_ostream<CharType, Traits>& operator<<(basic_ostream<CharType, Traits>& _Ostr, const basic_string<CharType, Traits, Allocator>& str);
參數
_Ostr
要寫入至輸出資料流。
str
要輸入到輸出資料流的字串。
傳回值
將指定的字串值寫入輸出資料流_Ostr
。
備註
範本函式多載運算子來插入物件 _ Str樣板類別的basic_string到資料流_ Ostr。 函式會有效地傳回_ Ostr。 write( _ Str. c_str, _ Str. size).
運算子>
測試運算子左邊的字串物件是否大於右邊的字串物件。
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);
參數
left
C-style 字串或物件型別的basic_string
進行比較。
right
C-style 字串或物件型別的basic_string
進行比較。
傳回值
true如果運算子左邊的字串物件進行字彙上大於右邊的字串物件,否則false。
備註
字串的字彙比較比較它們,直到逐字元︰
找到兩個對應的字元不相等,並比較其結果會被視為字串之間比較的結果。
它會尋找任何不等式,但一個字串有字元數超過一個,以及較短的字串會被視為小於較長的字串。
它會尋找任何不等式會發現,此連接字串具有相同數目的字元,因此是相等的字串。
範例
// 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.
運算子>=
測試運算子左邊的字串物件是否大於或等於右邊的字串物件。
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);
參數
left
C-style 字串或物件型別的basic_string
進行比較。
right
C-style 字串或物件型別的basic_string
進行比較。
傳回值
true如果運算子左邊的字串物件進行字彙上大於或等於右邊的字串物件,否則false。
備註
字串的字彙比較比較它們,直到逐字元︰
找到兩個對應的字元不相等,並比較其結果會被視為字串之間比較的結果。
它會尋找任何不等式,但一個字串有字元數超過一個,以及較短的字串會被視為小於較長的字串。
它會尋找任何不等式尋找字串有相同數目的字元,並因此是相等的字串。
範例
// 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.
運算子>>
樣板函式會從輸入資料流讀取的字串。
template <class CharType, class Traits, class Allocator>
basic_istream<CharType, Traits>& operator>>(
basic_istream<CharType, Traits>& _Istr,
basic_string<CharType, Traits, Allocator>& right);
參數
_Istr
用來擷取序列的輸入資料流
right
正在從輸入資料流擷取字串。
傳回值
讀取從指定的字串值_Istr
,並將它傳回right.
備註
除非運算子略過開頭空白字元skipws
旗標設定。 它會讀取下的所有字元,直到下一個字元是以泛空白字元或檔案結尾為止。
範本函式多載**運算子 >>**來取代所控制的序列right
序列的項目資料流擷取_Istr
。 會停止擷取︰
在檔案結尾。
函式擷取之後
_Istr
。 寬度項目,如果該值不是零。
函式擷取之後_Istr
。 max_size項目。
- 函式擷取項目之後ch的use_facet< ctype< CharType> > (
getloc
)。 是( ctype< CharType>::空間, ch) 為 true,字元會被放回。
如果函式會不擷取任何元素,則會呼叫setstate( ios_base::failbit
)。 在任何情況下,它會呼叫istr。 寬度(0),並傳回*這。
範例
// 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;
}