d_last_not_of()函数:
1.在字符串中查找最后一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
?size_type find_last_not_of( const char *str, size_type index, size_type num );
2.在字符串中查找最后一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始,最多查找num个字符如果没找到就返回string::nops
3.在字符串中查找最后一个与ch不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
?
size_type rfind( const basic_string &str, size_type index );
size_type rfind( const char *str, size_type index );
size_type rfind( const char *str, size_type index, size_type num );
size_type rfind( char ch, size_type index );
rfind()函数: (逆向查找)
1.返回最后一个与str中的某个字符匹配的字符,从index开始查找。如果没找到就返回string::npos
2.返回最后一个与str中的某个字符匹配的字符,从index开始查找,最多查找num个字符。如果没找到就返回string::npos
3.返回最后一个与ch匹配的字符,从index开始查找。如果没找到就返回string::npos
*/
void test3()
{?
?string str1( "Alpha Beta Gamma Delta" );
?unsigned int loc = str1.find( "Omega", 0 );
?if( loc != string::npos )
??cout << "Found Omega at " << loc << endl;
?else
??cout << "Didn't find Omega" << endl;
/*?例如,在下列代码中第一次调用rfind()返回string::npos,因为目标词语不在开始的8个字符中。然而,第二次调用返回9,因为目标词语在开始的20个字符之中。 */
?int loc2;
?string s = "My cat's breath smells like cat food.";
?loc2 = s.rfind( "breath", 8 );
?cout << "The word breath is at index " << loc2 << endl;//-1
?loc2 = s.rfind( "breath", 20 );
?cout << "The word breath is at index " << loc2 << endl;//9
}?
//插入(insert),替换(replace)
/*
iterator insert( iterator i, const char &ch );
basic_string &insert( size_type index, const basic_string &str );
basic_string &insert( size_type index, const char *str );
basic_string &insert( size_type index1, const basic_string &str, size_type index2, size_type num );
basic_string &insert( size_type index, const char *str, size_type num );
basic_string &insert( size_type index, size_type num, char ch );
void insert( iterator i, size_type num, const char &ch );
void insert( iterator i, iterator start, iterator end );
insert()函数的功能非常多:
1.在迭代器i表示的位置前面插入一个字符ch,
2.在字符串的位置index插入字符串str,
3.在字符串的位置index插入字符串str的子串(从index2开始,长num个字符),
4.在字符串的位置index插入字符串str的num个字符,
5.在字符串的位置index插入num个字符ch的拷贝,
6.在迭代器i表示的位置前面插入num个字符ch的拷贝,
7.在迭代器i表示的位置前面插入一段字符,从start开始,以end结束.
basic_string &replace( size_type index, size_type num, const basic_string &str );
basic_string &replace( size_type index1, size_type num1, const basic_string &str, size_type index2,
size_type num2 );
basic_string &replace( size_type index, size_type num, const char *str );
basic_string &replace( size_type index, size_type num1, const char *str, size_type num2 );
basic_string &replace( size_type index, size_type num1, size_type num2, char ch );
basic_string &replace( iterator start, iterator end, const basic_string &str );
basic_string &replace( iterator start, iterator end, const char *str );
basic_string &replace( iterator start, iterator end, const char *str, size_type num );
basic_string &replace( iterator start, iterator end, size_type num, char ch );
replace()函数:
1.用str中的num个字符替换本字符串中的字符,从index开始
2.用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,最多num1个字符
3.用str中的num个字符(从index开始)替换本字符串中的字符
4.用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,num1个字符
5.用num2个ch字符替换本字符串中的字符,从index开始
6.用str中的字符替换本字符串中的字符,迭代器start和end指示范围
7.用str中的num个字符替换本字符串中的内容,迭代器start和end指示范围,
8.用num个ch字符替换本字符串中的内容,迭代器start和end指示范围.
*/
void test4()
{??
?string s = "They say he carved it himself...from a BIGGER spoon";
?string s2 = "find your Homer.";
?s.replace( 32, s2.length(), s2 );
?cout << s << endl;?? //They say he carved it himself...find your Homer.oon
}?
//其他
/*
交换(swap)? void swap( basic_string &str );//把str和本字符串交换
?
basic_string substr( size_type index, size_type num = npos );
substr()返回本字符串的一个子串,从index开始,长num个字符。如果