basic_string::find
앞으로 지정 된 문자 시퀀스와 일치 하는 부분 문자열의 처음 나오는 문자열을 검색 합니다.
size_type find(
value_type _Ch,
size_type _Off = 0
) const;
size_type find(
const value_type* _Ptr,
size_type _Off = 0
) const;
size_type find(
const value_type* _Ptr,
size_type _Off,
size_type _Count
) const;
size_type find(
const basic_string<CharType, Traits, Allocator>& _Str,
size_type _Off = 0
) const;
매개 변수
_Ch
문자 값 멤버 함수 검색 됩니다._Off
검색이 시작 되는 위치의 인덱스입니다._Ptr
-문자열 멤버 함수 검색 됩니다 C._Count
앞으로 계산 하 여 멤버 함수 검색 되어 C 문자열에서 첫 번째 문자에서 문자의 수입니다._Str
멤버 함수 검색 되는 문자열입니다.
반환 값
로그인이 성공 하면 부분 문자열의 첫 번째 문자의 인덱스에 대 한 검색. 그렇지 않으면 npos.
예제
// basic_string_find.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 ( "Hello Everyone" );
cout << "The original string str1 is: " << str1 << endl;
basic_string <char>::size_type indexCh1a, indexCh1b;
indexCh1a = str1.find ( "e" , 3 );
if (indexCh1a != string::npos )
cout << "The index of the 1st 'e' found after the 3rd"
<< " position in str1 is: " << indexCh1a << endl;
else
cout << "The character 'e' was not found in str1 ." << endl;
indexCh1b = str1.find ( "x" );
if (indexCh1b != string::npos )
cout << "The index of the 'x' found in str1 is: "
<< indexCh1b << endl << endl;
else
cout << "The Character '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 ( "Let me make this perfectly clear." );
cout << "The original string str2 is: " << str2 << endl;
basic_string <char>::size_type indexCh2a, indexCh2b;
const char *cstr2 = "perfect";
indexCh2a = str2.find ( cstr2 , 5 );
if ( indexCh2a != string::npos )
cout << "The index of the 1st element of 'perfect' "
<< "after\n the 5th position in str2 is: "
<< indexCh2a << endl;
else
cout << "The substring 'perfect' was not found in str2 ."
<< endl;
const char *cstr2b = "imperfectly";
indexCh2b = str2.find ( cstr2b , 0 );
if (indexCh2b != string::npos )
cout << "The index of the 1st element of 'imperfect' "
<< "after\n the 5th position in str3 is: "
<< indexCh2b << endl;
else
cout << "The substring 'imperfect' was not found in str2 ."
<< endl << endl;
// The third member function searches a string
// for a substring as specified by a C-string
string str3 ( "This is a sample string for this program" );
cout << "The original string str3 is: " << str3 << endl;
basic_string <char>::size_type indexCh3a, indexCh3b;
const char *cstr3a = "sample";
indexCh3a = str3.find ( cstr3a );
if ( indexCh3a != string::npos )
cout << "The index of the 1st element of sample "
<< "in str3 is: " << indexCh3a << endl;
else
cout << "The substring 'perfect' was not found in str3 ."
<< endl;
const char *cstr3b = "for";
indexCh3b = str3.find ( cstr3b , indexCh3a + 1 , 2 );
if (indexCh3b != string::npos )
cout << "The index of the next occurrence of 'for' is in "
<< "str3 begins at: " << indexCh3b << endl << endl;
else
cout << "There is no next occurrence of 'for' in str3 ."
<< endl << endl;
// The fourth member function searches a string
// for a substring as specified by a string
string str4 ( "clearly this perfectly unclear." );
cout << "The original string str4 is: " << str4 << endl;
basic_string <char>::size_type indexCh4a, indexCh4b;
string str4a ( "clear" );
indexCh4a = str4.find ( str4a , 5 );
if ( indexCh4a != string::npos )
cout << "The index of the 1st element of 'clear' "
<< "after\n the 5th position in str4 is: "
<< indexCh4a << endl;
else
cout << "The substring 'clear' was not found in str4 ."
<< endl;
string str4b ( "clear" );
indexCh4b = str4.find ( str4b );
if (indexCh4b != string::npos )
cout << "The index of the 1st element of 'clear' "
<< "in str4 is: "
<< indexCh4b << endl;
else
cout << "The substring 'clear' was not found in str4 ."
<< endl << endl;
}
요구 사항
헤더: <string>
네임 스페이스: std