共用方式為


char_traits::find

搜尋指定的字元的第一個字元的範圍。

static const char_type* find( 
   const char_type* _Str,  
   size_t _Num,  
   const char_type& _Ch  
);

參數

  • _Str
    在將搜尋字串的第一個字元。

  • _Num
    位置數目,計算以開始,在要搜尋的範圍。

  • _Ch
    在範圍中搜尋的字元。

傳回值

對指定的字元第一次出現的指標在範圍內,如果找不到相符項目;否則, null 指標。

範例

// char_traits_find.cpp
// compile with: /EHsc
#include <string>
#include <iostream>

int main( ) 
{
   using namespace std;

   const char* s1 = "f2d-1234-abcd";
   const char* result1;
   cout << "The string to be searched is: " << s1 << endl;

   // Searching for a 'd' in the first 6 positions of string s1
   result1 = char_traits<char>::find ( s1 , 6 , 'd');
   cout << "The character searched for in s1 is: "
        << *result1 << endl;
   cout << "The string beginning with the first occurrence\n "
        << "of the character 'd' is: " << result1 << endl;

   // When no match is found the NULL value is returned
   const char* result2;
   result2 = char_traits<char>::find ( s1 , 3 , 'a');
   if ( result2 == NULL )
      cout << "The result2 of the search is NULL." << endl;
   else
      cout << "The result2 of the search  is: " << result1
           << endl;
}
  

需求

標頭:<string>

命名空間: std

請參閱

參考

char_traits 結構