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