公共靜態成員函式
<string>

std::char_traits::eq

static bool eq (const char_type& c, const char_type& d);
static constexpr bool eq (char_type c, char_type d) noexcept;
比較字元是否相等
返回字元cd是否被認為是相等的。

char_traits 的標準特化中,此函式的行為與內建的行為相同`operator==`,但自定義的字元特徵類可能提供替代行為。
char_traits 的標準特化中,此函式的行為與內建的行為相同`operator==`對於型別無符號字元,但自定義的字元特徵類可能提供替代行為。

引數

c, d
字元值。
成員型別char_type字元型別(即,char_traits 中的類模板引數)。

返回值

如果c被認為等於d

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// char_traits::eq
#include <iostream>   // std::cout
#include <string>     // std::basic_string, std::char_traits
#include <cctype>     // std::tolower
#include <cstddef>    // std::size_t

// traits with case-insensitive eq:
struct custom_traits: std::char_traits<char> {
  static bool eq (char c, char d) { return std::tolower(c)==std::tolower(d); }
  // some (non-conforming) implementations of basic_string::find call this instead of eq:
  static const char* find (const char* s, std::size_t n, char c)
  { while( n-- && (!eq(*s,c)) ) ++s; return s; }
};

int main ()
{
  std::basic_string<char,custom_traits> str ("Test");
  std::cout << "T found at position " << str.find('t') << '\n';
  return 0;
}

輸出
T found at position 0


複雜度

常量。

異常安全

無異常丟擲保證:此成員函式在任何標準特化中都不會丟擲異常。

另見