public static member function
<string>

std::char_traits::find

static const char_type* find (const char_type* p, size_t n, const char_type& c);
查詢字元的首次出現
返回指向 p 指向的 n 個字元序列中與 c 相等的第一個字元的指標。

所有字元特徵型別都應實現該函式,就像使用成員 eq比較各個字元一樣。

引數

p
指向包含字元序列的陣列的指標。
請注意,該函式將認為序列的長度為 n 個字元,而不管它是否包含空字元。
n
要比較的字元序列的長度(以字元為單位)。
c
一個字元值。

成員型別char_type字元型別(即,char_traits 中的類模板引數)。
size_t 是一個無符號整數型別。

返回值

如果存在第一個匹配項,則為指向該匹配項的指標;否則為空指標

示例

1
2
3
4
5
6
7
8
9
10
11
// char_traits::find
#include <iostream>   // std::cout
#include <string>     // std::char_traits

int main ()
{
  const char foo[] = "test string";
  const char* p = std::char_traits<char>::find(foo,std::char_traits<char>::length(foo),'i');
  if (p) std::cout << "the first 'i' in \"" << foo << "\" is at " << (p-foo) << ".\n";
  return 0;
}

輸出
the first 'i' in "test string" is at 8.


複雜度

最多為 n 的線性關係。

異常安全

除非 p 指向的陣列不夠長,否則此成員函式在任何標準特化中都不會引發異常(無丟擲保證)。
否則,將導致未定義行為

另見