若要檢視英文版的文章,請選取 [原文] 核取方塊。您也可以將滑鼠指標移到文字上,即可在快顯視窗顯示英文原文。
譯文
原文

basic_string::find_first_not_of

 

將字串搜尋不是指定字串的元素的第一個字元。

size_type find_first_not_of(
   value_type _Ch, 
   size_type _Off = 0
) const;
size_type find_first_not_of(
   const value_type* _Ptr,
   size_type _Off = 0
) const;
size_type find_first_not_of(
   const value_type* _Ptr, 
   size_type _Off,
   size_type _Count
) const;
size_type find_first_not_of(
   const basic_string<CharType, Traits, Allocator>& _Str,
   size_type _Off = 0
) const;

_Ch

搜尋成員函式的字元值。

_Off

開始搜尋的索引位置。

_Ptr

用以搜尋成員函式的 C 字串。

_Count

字元數目,以第一個字元開始往前計算,用於搜尋成員函式的 C 字串。

_Str

用於搜尋成員函式的字串。

在成功時,為搜尋指定的子字串的第一個字元的索引;否則為 npos

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

int main( ) 
{
   using namespace std;

   // The first member function
   // searches for a single character in a string
   string str1 ( "xddd-1234-abcd" );
   cout << "The original string str1 is: " << str1 << endl;
   basic_string <char>::size_type indexCh1a, indexCh1b;
   static const basic_string <char>::size_type npos = -1;

   indexCh1a = str1.find_first_not_of ( "d" , 2 );
   if ( indexCh1a != npos )
      cout << "The index of the 1st 'd' found after the 3rd"
           << " position in str1 is: " << indexCh1a << endl;
   else
      cout << "The character 'd' was not found in str1 ." << endl;

   indexCh1b = str1.find_first_not_of  ( "x" );
   if (indexCh1b != npos )
      cout << "The index of the 'non x' found in str1 is: " 
           << indexCh1b << endl << endl;
   else
      cout << "The character 'non x' was not found in str1."
           << endl << endl;

   // The second member function searches a string
   // for a substring as specified by a C-string
   string str2 ( "BBB-1111" );
   cout << "The original string str2 is: " << str2 << endl;
   basic_string <char>::size_type indexCh2a, indexCh2b;

   const char *cstr2 = "B1";
   indexCh2a = str2.find_first_not_of ( cstr2 , 6 );
   if ( indexCh2a != npos )
      cout << "The index of the 1st occurrence of an "
           << "element of 'B1' in str2 after\n the 6th "
           << "position is: " << indexCh2a << endl;
   else
      cout << "Elements of the substring 'B1' were not"
           << "\n found in str2 after the 6th position."
           << endl;

   const char *cstr2b = "B2";
   indexCh2b = str2.find_first_not_of ( cstr2b );
   if ( indexCh2b != npos )
      cout << "The index of the 1st element of 'B2' "
           << "after\n the 0th position in str2 is: "
           << indexCh2b << endl << endl;
   else
      cout << "The substring 'B2' was not found in str2 ." 
           << endl << endl << endl;

   // The third member function searches a string
   // for a substring as specified by a C-string
   string str3 ( "444-555-GGG" );
   cout << "The original string str3 is: " << str3 << endl;
   basic_string <char>::size_type indexCh3a, indexCh3b;

   const char *cstr3a = "45G";
   indexCh3a = str3.find_first_not_of ( cstr3a );
   if ( indexCh3a != npos )
      cout << "The index of the 1st occurrence of an "
           << "element in str3\n other than one of the "
           << "characters in '45G' is: " << indexCh3a 
           << endl;
   else
      cout << "Elements in str3 contain only characters "
           << " in the string '45G'. "
           << endl;

   const char *cstr3b = "45G";
   indexCh3b = str3.find_first_not_of ( cstr3b , indexCh3a + 1 , 2 );
   if ( indexCh3b != npos )
      cout << "The index of the second occurrence of an "
           << "element of '45G' in str3\n after the 0th "
           << "position is: " << indexCh3b << endl << endl;
   else
      cout << "Elements in str3 contain only characters "
           << " in the string  '45G'. "
           << endl  << endl;

   // The fourth member function searches a string
   // for a substring as specified by a string
   string str4 ( "12-ab-12-ab" );
   cout << "The original string str4 is: " << str4 << endl;
   basic_string <char>::size_type indexCh4a, indexCh4b;

   string str4a ( "ba3" );
   indexCh4a = str4.find_first_not_of ( str4a , 5 );
   if (indexCh4a != npos )
      cout << "The index of the 1st non occurrence of an "
           << "element of 'ba3' in str4 after\n the 5th "
           << "position is: " << indexCh4a << endl;
   else
      cout << "Elements other than those in the substring"
           << " 'ba3' were not found in the string str4."
           << endl;

   string str4b ( "12" );
   indexCh4b = str4.find_first_not_of ( str4b  );
   if (indexCh4b != npos )
      cout << "The index of the 1st non occurrence of an "
           << "element of '12' in str4 after\n the 0th "
           << "position is: " << indexCh4b << endl;
   else
      cout << "Elements other than those in the substring"
           << " '12' were not found in the string str4."
           << endl;
}
原始字串 str1 如下:xddd-1234-abcd
」中的第 1 個「d 的索引,在 str1 的第 3 個位置是之後:4
在 str1 "中這個非「x 的索引是:1

原始字串 str2 如下:BBB-1111
子字串「B1 "項目不是
 位於 str2 在步驟 6 之後的位置。
「B2」的第 1 個元素的索引之後
 在 str2 的 0th 位置為:3

原始字串 str3 如下:444 555 時鐘晶片 GGG
項目的第 1 次出現的索引。str3 的
 刪除其中一個「45G "字元如下:3
「45G "項目中的第二次出現的索引。str3
 在 0th 位置之後:7

原始字串 str4 如下:12 ab 12 ab
「ba3」的元素的第 1 非項目的索引之後 str4
 第 5 位置為:5
項目的第 1 非事件索引的「12 "之後 str4
 0th 位置為:2

需求

標頭:<string>

命名空間: std

顯示: