public member function
<string>

std::basic_string::compare

string (1)
int compare (const basic_string& str) const;
substrings (2)
int compare (size_type pos, size_type len, const basic_string& str) const;int compare (size_type pos, size_type len, const basic_string& str,             size_type subpos, size_type sublen) const;
c-string (3)
int compare (const charT* s) const;int compare (size_type pos, size_type len, const charT* s) const;
buffer (4)
int compare (size_type pos, size_type len, const charT* s, size_type n) const;
string (1)
int compare (const basic_string& str) const noexcept;
substrings (2)
int compare (size_type pos, size_type len, const basic_string& str) const;int compare (size_type pos, size_type len, const basic_string& str,             size_type subpos, size_type sublen) const;
c-string (3)
int compare (const charT* s) const;int compare (size_type pos, size_type len, const charT* s) const;
buffer (4)
int compare (size_type pos, size_type len, const charT* s, size_type n) const;
string (1)
int compare (const basic_string& str) const noexcept;
substrings (2)
int compare (size_type pos, size_type len, const basic_string& str) const;int compare (size_type pos, size_type len, const basic_string& str,             size_type subpos, size_type sublen = npos) const;
c-string (3)
int compare (const charT* s) const;int compare (size_type pos, size_type len, const charT* s) const;
buffer (4)
int compare (size_type pos, size_type len, const charT* s, size_type n) const;
比較字串
basic_string 物件(或子字串)的值與其引數指定的字元序列進行比較。

被比較的字串basic_string 物件的值,或者 - 如果使用的簽名具有 poslen 引數 - 是從位置 pos 的字元開始並跨越 len 個字元的子字串。

此字串與一個 比較字串 進行比較,該字串由傳遞給函式的其他引數確定。

這些序列使用 traits_type::compare 進行比較。

引數

str
另一個具有相同型別的basic_string物件(具有相同的類模板引數charT, 特性 (traits)Alloc),完全(或部分)用作比較字串
pos
被比較字串中第一個字元的位置。
如果它大於字串長度,則丟擲out_of_range
注意:第一個字元用值表示0(不是1).
len
被比較字串的長度(如果字串較短,則為儘可能多的字元)。
basic_string::npos值表示直到字串末尾的所有字元。
subpos, sublen
與上面的 poslen 相同,但用於比較字串
s
指向字元陣列的指標。
如果指定了引數 n (4),則陣列中的前 n 個字元用作比較字串
否則 (3),預期為 null 終止的序列:要用作比較字串的字元序列的長度由第一個 null 字元的出現確定。
n
要比較的字元數。

charTbasic_string 的字元型別(即,它的第一個模板引數)。
成員型別size_type是一種無符號整型型別。

返回值

返回一個有符號整數,指示字串之間的關係
被比較字串比較字串之間的關係
0它們比較相等
<0被比較字串中第一個不匹配的字元的值較低,或者所有比較的字元都匹配,但被比較字串較短。
>0被比較字串中第一個不匹配的字元的值較高,或者所有比較的字元都匹配,但被比較字串較長。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// comparing apples with apples
#include <iostream>
#include <string>

int main ()
{
  std::string str1 ("green apple");
  std::string str2 ("red apple");

  if (str1.compare(str2) != 0)
    std::cout << str1 << " is not " << str2 << '\n';

  if (str1.compare(6,5,"apple") == 0)
    std::cout << "still, " << str1 << " is an apple\n";

  if (str2.compare(str2.size()-5,5,"apple") == 0)
    std::cout << "and " << str2 << " is also an apple\n";

  if (str1.compare(6,5,str2,4,5) == 0)
    std::cout << "therefore, both are apples\n";

  return 0;
}

輸出
green apple is not red apple
still, green apple is an apple
and red apple is also an apple
therefore, both are apples


複雜度

未指定,但通常高達被比較的比較的字串長度中的線性。

迭代器有效性

沒有變化。

資料競爭

該物件被訪問。

異常安全

強保證:如果丟擲異常,basic_string 中沒有變化(除了 (1),保證不會丟擲異常)。

如果已知s未指向足夠長的陣列,會導致未定義行為

如果pos大於字串長度,或者subpos大於str長度,則會丟擲out_of_range異常。

另見