find()
语法:
size_type find( const basic_string &str, size_type index ); size_type find( const char *str, size_type index ); size_type find( const char *str, size_type index, size_type length ); size_type find( char ch, size_type index ); |
find()函数:
返回str在字符串中第一次出现的位置(从index开始查找)。如果没找到则返回string::npos,
返回str在字符串中第一次出现的位置(从index开始查找,长度为length)。如果没找到就返回string::npos,
返回字符ch在字符串中第一次出现的位置(从index开始查找)。如果没找到就返回string::npos
find_first_of
语法:
size_type find_first_of( const basic_string &str, size_type index = 0 ); size_type find_first_of( const char *str, size_type index = 0 ); size_type find_first_of( const char *str, size_type index, size_type num ); size_type find_first_of( char ch, size_type index = 0 ); |
find_first_of()函数:
查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,如果没找到就返回string::npos
查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,最多搜索num个字符。如果没找到就返回string::npos,
查找在字符串中第一个与ch匹配的字符,返回它的位置。搜索从index开始。
size_type find_first_not_of( const basic_string &str, size_type index = 0 ); size_type find_first_not_of( const char *str, size_type index = 0 ); size_type find_first_not_of( const char *str, size_type index, size_type num ); size_type find_first_not_of( char ch, size_type index = 0 ); |
find_first_not_of()函数:
在字符串中查找第一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
在字符串中查找第一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始,最多查找num个字符。如果没找到就返回string::nops
在字符串中查找第一个与ch不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
find_last_of
语法:
size_type find_last_of( const basic_string &str, size_type index = npos ); size_type find_last_of( const char *str, size_type index = npos ); size_type find_last_of( const char *str, size_type index, size_type num ); size_type find_last_of( char ch, size_type index = npos ); |
find_last_of()函数:
在字符串中查找最后一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
在字符串中查找最后一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,最多搜索num个字符。如果没找到就返回string::nops
在字符串中查找最后一个与ch匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
find_last_not_of
语法:
size_type find_last_not_of( const basic_string &str, size_type index = npos ); size_type find_last_not_of( const char *str, size_type index = npos); size_type find_last_not_of( const char *str, size_type index, size_type num ); size_type find_last_not_of( char ch, size_type index = npos ); |
find_last_not_of()函数:
在字符串中查找最后一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
在字符串中查找最后一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始,最多查找num个字符如果没找到就返回string::nops
在字符串中查找最后一个与ch不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
这些函数都是异曲同工。 对于单字符的函数来说。是查找字符在str里第一次出现的位置。
对于字符串或者数组来说,是查找字符或数字任意字符在str第一次匹配的位置。 这些函数索引都是从0开始数。
string str1("heartbeat");
string str2("abcde");
int npos=0;
npos=str1.find_first_of(str2,0);
cout<<str2<<" in "<<str1<<" position is "<<npos<<endl; // 1
npos=str1.find_first_of('e',2);
cout<<'e'<<" in "<<str1<<" position is "<<npos<<endl; //6
npos=str1.find_first_of(str2,2);
cout<<str2<<" in "<<str1<<" position is "<<npos<<endl; //2
char charValue[]={'a','e','i','o','u'};
npos=str1.find_first_of(charValue,4,2); //最后的参数 是限定数组中参与查找的个数
cout<<"aeiou"<<" in "<<str1<<" position is "<<npos<<endl; //6
其实这些函数只要记住,索引从零开始。就会用了。