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