字符
8)size_tfind_last_not_of (const char* s, size_t pos = npos) const;
//在当前字符串的pos索引位置开始,查找最后一个不位于子串s的字符,返回找到的位置索引,-1表示查找不到子串
#include
#include
using namespace std; int main(void) { //0123456789012345678901234 string s(dog bird chicken bird cat); //字符串查找 cout << s.find(bird) << endl; //打印 cout << (int)s.find(pig) << endl; //打印-1 //字符查找 cout << s.find('i',7) << endl; //打印 //从字符串的末尾开始查找字符串 cout << s.rfind(bird) << endl; //打印 //从字符串的末尾开始查找字符 cout << s.rfind('i') << endl; //打印 //查找第个属于某子串的字符 cout << s.find_first_of(13r98) << endl; //找到字符r,打印 //查找第个不属于某字符串的字符 cout << s.find_first_not_of(dog bird 2006) << endl; //找到字符c,打印 //查找最后一个属于某子串的字符 cout << s.find_last_of(13r98) << endl; //字符r,打印 //查找最后一个不属于某字符串的字符 cout << s.find_last_not_of(tea) << endl; //字符c,打印 return 0; }

字符串的比较
1)int compare (conststring& str) const;//将当前字符串与字符串str比较,
相等返回0,大于str返回1,小于str返回-1
2)int compare (size_tpos, size_t len, const char* s) const; //将当前字符串从
Pos索引位置开始的len个字符构成的字符串与字符串s比较,
相等返回0,大于str返回1,小于str返回-1
3)int compare (constchar* s) const; //直接将当前字符串与字符串s比较,
相等返回0
#include
#include
using namespace std; int main(void) { string s1(abcdef); string s2(abc); cout << s1.compare(abcdef) << endl; //相等,打印 cout << s1.compare(s2) << endl; //s1 > s2,打印 cout << s1.compare(abyz) << endl; //s1 < abyz,打印-1 cout << s1.compare(0,3,s2) << endl; //s1的前个字符==s2,打印 return 0; }
?