构造函数
string(); //创建一个默认的 string 对象,这个字符串的长度为0
string str;
string(const char *s); //创建一个string对象,并初始化字符串为指针 s 指向的字符串
string str("Hello World");
string(const char *s, int n); //创建一个string对象,并初始化字符串为指针 s 指向的前 n 个字符
string str("123456", 2); //str = "12"
string(const string &str); //创建一个 string 对象,并初始化字符串为 str
string str1("Hello World");
string str2(str1);
string(const string& str, int pos); //创建一个 string 对象,并初始化字符串为 str 中下标从 pos 开始的后面所有字符
string str1("123456");
string str2(str1, 3); //str = "456"
string(const string& str, int pos, int n); //创建一个 string 对象,并初始化字符串为 str 中下标从 pos开始的前 n 个字符
string str1("123456");
string str2(str1, 3, 2); //str = "45"
string(int n,char c); //创建一个 string 对象,并初始化字符串为 n 个字符 c
string str(5, 'h'); //str = "hhhhh"
字符串赋值
string& operator=(const string& str); //将 str 赋值给另一个 string 对象
string str1("Hello World");
string str2 = str1; 将str1赋值给str2对象
string& operator=(const char* s); //将指针 s 指向的字符串赋值给 string 对象
string str;
str = "Hello World";
string& operator=(char c); //将字符 c 赋值给 string 对象
string str;
str = 'h';
string& assign(int n, char c); //将 n 个字符 c 赋值给 string 对象
string str;
str.assign(5, 'h'); //str = "hhhhh"
string& assign(const string& str); //将 str 赋值给 string 对象
string str1 = "Hello World";
string str2;
str2.assign(str1); //str2 = "Hello World"
string& assign(const string& str, int pos, int n); //将 str 从下标 pos 开始的前 n 个字符赋值给 string对象
string str1 = "123456";
string str2;
str2.assign(str1, 3, 2); //str2 = "45"
string& assign(const char* s); //将指针 s 指向的字符串赋值给 string 对象
string str;
str.assign("123456"); //str = "123456"
string& assign(const char* s, int n); //将指针 s 指向的字符串的前 n 个字符赋值给 string 对象
string str;
str.assign("123456", 3); //str = "123"
元素访问
char& at(int pos); //返回字符串变量位于指定下标 pos 的字符的引用
string str("123456");
char c = str.at(1); // c = '2'
const char& at(int pos) const; //返回字符串常量位于指定下标 pos 的字符的引用
const string str("123456");
const char c = str.at(1); // c = '2'
char& operator[](int pos); //返回字符串变量位于指定下标 pos 的字符的引用
string str("123456");
char c = str[1]; // c = '2'
const char& operator[](int pos) const; //返回字符串常量位于指定下标 pos 的字符的引用
const string str("123456");
const char c = str[1]; // c = '2'
char& front(); //返回字符串变量首字符的引用(C++11起)
string str("123456");
char c = str.front(); // c = '1'
const char& front() const; //返回字符串常量首字符的引用(C++11起)
const string str("123456");
const char c = str.front(); // c = '1'
char& back(); //返回字符串变量末字符的引用(C++11起)
string str("123456");
char c = str.back(); // c = '6'
const char& back() const; //返回字符串常量末字符的引用(C++11起)
const string str("123456");
const char c = str.back(); // c = '6'
char* data(); //返回指向字符串变量首字符的指针(C++17起)
string str("123456");
char *p = str.data(); //C++17标准才支持的
const char* data() const; //返回指向字符串常量首字符的指针
const string str("123456");
const char *p = str.data();
容量
bool empty() const; //判断字符串是否为空,为空返回true,否则返回false
string str;
if (str.empty())
cout << "str is empty" << endl;
int size() const;或 int length() const; //返回字符串中字符的个数
string str("123456");
cout << "size = " << str.size() << endl; //size = 6
cout << "length = " << str.length() << endl; //length = 6
int max_size() const; //返回一个字符串对象能够存储字符个数的最大值
string str;
//max_size = 4611686018427387897(64位系统测试)
cout << "max_size = " << str.max_size() << endl;
操作
void clear(); //清空字符串
string str("123456");
cout << "size = " << str.size() << endl; //size = 6
str.clear();
cout << "size = " << str.size() << endl; //size = 0
string& insert(int index, int n, char c); //从下标 index 开始,插入 n 个字符 c
string str("123456");
str.insert(3, 5, 'h');
cout << str << endl; //str = "123hhhhh456"
string& insert(int index, const char* s); //从下标 index 开始,插入指针s指向的字符串
string str("123456");
str.insert(3, "test");
cout << str << endl; //str = "123test456"
string& insert(int index, const char* s, int n); //从下标 index 开始,插入指针 s 指向的前 n 个字符
string str("123456");
str.insert(3, "test", 2);
cout << str << endl; //str = "123te456"
string& insert(int index, const string& str); //从下标 index 开始,插入 str 字符串
string str1("123456");
string str2("test");
str2.insert(3, str1);
cout << str2 << endl; //str2 = "tes123456t"
string& insert(int index, const string& str, int index_str, int n); //从下标 index 开始,插入 str 字符串中从下标 index_str 开始的前 n 个字符
string str1("123456");
string str2("test");
str2.insert(3, str1, 1, 3);
cout << str2 << endl; //str2 = "tes234t"
string& erase(int index = 0, int n = npos); //从下标 index 开始,移除 n 个字符,如果不传参数,默认会将整个字符串清空,这时跟clear()函数的功能一样
string str1("123456");
str1.erase();
cout << str1 << endl; //str1 = ""
string str2("hello");
str2.erase(1, 2);
cout << str2 << endl; //str2 = "hlo"
void push_back(char c); //往字符串末尾附加字符 c
string str("123456");
str.push_back('h');
cout << str << endl; //str = "123456h"
void pop_back(); //移除字符串末尾字符(C++11起)
string str("123456");
str.pop_back();
cout << str << endl; //str = "12345"
string& append(int n, char c); //往字符串末尾附加 n 个字符 c
string str("123456");
str.append(5, 'h');
cout << str << endl; //str = "123456hhhhh"
string& append(const string& str); //往字符串末尾附加字符串str
string str1("123456");
string str2 = "test";
str2.append(str1);
cout << str2 << endl; //str2 = "test123456"
string& append(const string& str, int pos, int n); //往字符串末尾附加字符串str中从下标pos开始的前n个字符
string str1("123456");
string str2 = "test";
str2.append(str1, 1, 3);
cout << str2 << endl; //str2 = "test234"
string& append(const char* s); //往字符串末尾附加指针 s 指向的字符串
string str("123456");
str.append("test");
cout << str << endl; // str = "123456test"
string& append(const char* s, int n); //往字符串末尾附加指针 s 指向的字符串的前n个字符
string str("123456");
str.append("test", 2);
cout << str << endl; // str = "123456te"
string& operator+=(const string& str); //往字符串末尾附加字符串str
string str1("123456");
string str2("test");
str1 += str2;
cout << str1 << endl; // str = "123456test"
string& operator+=(char c); //往字符串末尾附加字符 c
string str1("123456");
char c = 'h';
str1 += c;
cout << str1 << endl; // str = "123456h"
string& operator+=(const char* s); //往字符串末尾附加指针 s 指向的字符串
string str1("123456");
str1 += "test";
cout << str1 << endl; // str = "123456test"
int compare(const string& str) const; //将字符串跟字符串常量 str 比较
string str1("test");
const string str2("test");
int r = str1.compare(str2);
if (r > 0)
cout << str1 << " > " << str2 << endl;
else if (r == 0)
cout << str1 << " = " << str2 << endl; //test = test
else
cout << str1 << " < " << str2 << endl;
int compare(int pos1, int n1, const string& str) const; //将字符串从下标 pos1 的前 n1 个字符跟字符串常量 str 比较
string str1("12test");
const string str2("test");
int r = str1.compare(2, 4, str2);
cout << "r = " << r << endl; //r = 0
int compare(int pos1, int n1, const string& str, int pos2, int n2) const; //将字符串从下标 pos1 开始的前 n1 个字符跟字符串常量 str 从下标 pos2 开始的前 n2 个字符进行比较
string str1("123test");
const string str2("5test4");
int r = str1.compare(3, 4, str2, 1, 4);
cout << "r = " << r << endl; //r = 0
int compare(const char* s) const; //将字符串跟指针 s 指向的字符串进行比较
string str("test");
int r = str.compare("test");
cout << "r = " << r << endl; //r = 0
int compare(int pos1, int n1, const char* s) const; //将字符串从下标pos1开始的前 n1 个字符跟指针 s 指向的字符串进行比较
string str("12test");
int r = str.compare(2, 4, "test");
cout << "r = " << r << endl; //r = 0
int compare(int pos1, int n1, const char* s, int n2) const; //将字符串从下标 pos1 开始的前 n1 个字符跟指针 s 指向的字符串的前 n2 个字符进行比较
string str("12test");
int r = str.compare(2, 4, "test", 4);
cout << "r = " << r << endl; //r = 0
string& replace(int pos, int n, const string& str); //将字符串从下标 pos 开始的前 n 个字符替换成 str
string str1("123456");
string str2("123456");
const string str3("test");
str1.replace(1, 1, str3);
str2.replace(1, 5, str3);
cout << str1 << endl; //str1 = "1test3456"
cout << str2 << endl; //str2 = "1test"
string& replace(int pos1, int n1, const string& str, int pos2, int n2); //将字符串从下标 pos1 开始的前 n1 个字符替换成字符串 str中从下标 pos2 开始的前 n2 个字符
string str1("123456");
string str2("123456");
const string str3("test");
str1.replace(1, 1, str3, 0, 2);
str2.replace(1, 5, str3, 2, 1);
cout << str1 << endl; //str1 = "1te3456"
cout << str2 << endl; //str2 = "1s"
string& replace(int pos, int n, const char* s); //将字符串从下标 pos 开始的前 n 个字符替换成指针 s 指向的字符串
string str1("123456");
string str2("123456");
str1.replace(1, 1, "test");
str2.replace(1, 5, "test");
cout << str1 << endl; //str1 = "1test3456"
cout << str2 << endl; //str2 = "1test"
string& replace(int pos1, int n1, const char* s, int n2); //将字符串从下标 pos1 开始的前 n1 个字符替换成指针 s 指向的字符串的前 n2 个字符
string str1("123456");
string str2("123456");
str1.replace(1, 1, "test", 2);
str2.replace(1, 5, "test", 3);
cout << str1 << endl; //str1 = "1te3456"
cout << str2 << endl; //str2 = "1tes"
string& replace( int pos1, int n1,int n2, char c); //将字符串从下标 pos1 开始的前 n1 个字符替换成 n2 个字符 c
string str1("123456");
string str2("123456");
str1.replace(1, 1, 2, 'h');
str2.replace(1, 5, 4, 'e');
cout << str1 << endl; //str1 = "1hh3456"
cout << str2 << endl; //str2 = "1eeee"
string substr(int pos = 0, int n = npos) const; //返回字符串中从下标pos开始的前n个字符这个子串
string str1("123456");
string str2 = str1.substr(); //默认会返回整个str1
string str3 = str1.substr(2, 3);
cout << str1 << endl; //str1 = "123456"
cout << str2 << endl; //str2 = "123456"
cout << str3 << endl; //str3 = "345"
int copy(char* dest, int n, int pos = 0) const; //将字符串从下标 pos 开始,拷贝前 n 个字符到 dest 中
string str("123456");
char buf[4] = {0};
str.copy(buf, 3, 1); //buf = "234"
cout << buf << endl;
memset(buf, 0, 4);
str.copy(buf, 3); //buf = "123", 默认pos = 0
cout << buf << endl;
void swap(string& str); //将字符串对象跟 str 进行交换
string str1("123456");
string str2("test");
str1.swap(str2);
cout << str1 << endl; //str1 = "test"
cout << str2 << endl; //str2 = "123456"
查找
int find(const string& str, int pos = 0) const; //字符串对象中从下标 pos 开始正向查找 str 第一次出现的位置,如果找到返回第一次出现的下标,没有找到则返回-1
string str1("123456789");
string str2("345");
int index = str1.find(str2);
cout << index << endl; //index = 2
index = str1.find(str2, 3); //从下标3开始正向查找,已找不到str2了
cout << index << endl; //index = -1
string str3("hi");
index = str1.find(str3);
cout << index << endl; //index = -1
int find(const char* s, int pos = 0) const; //字符串对象中从下标pos开始正向查找指针 s 指向的字符串第一次出现的位置,如果找到返回第一次出现的下标,没有找到则返回-1
string str("123456789");
int index = str.find("345");
cout << index << endl; //index = 2
index = str.find("345", 3); //从下标3开始正向查找,已找不到"345"了
cout << index << endl; //index = -1
index = str.find("hi");
cout << index << endl; //index = -1
int find(const char* s, int pos, int n) const; //字符串对象中从下标pos开始正向查找指针 s 指向的字符串的前 n 个字符子串第一次出现的位置,如果找到返回第一次出现的下标,没有找到则返回-1
string str("123456789");
int index = str.find("346", 0, 3);
cout << index << endl; //index = -1, 找不到"346"
index = str.find("346", 0, 2); //index = 2,找“346”中的子串“34”
cout << index << endl;
index = str.find("346", 3, 2); //从下标3开始正向查找,已找不到“346”中的子串“34”了
cout << index << endl; //index = -1
int find(char c, int pos = 0) const; //字符对象中从下标pos开始正向查找字符 c 第一次出现的位置,如果找到返回第一次出现的下标,没有找到则返回-1
string str("123456789");
int index = str.find('0');
cout << index << endl; //index = -1, 找不到'0'
index = str.find('3', 1); //index = 2
cout << index << endl;
index = str.find('3', 3); //从下标3开始正向查找,已找不到'3'
cout << index << endl; //index = -1
int rfind(const string& str, int pos = npos) const; //字符串对象中从下标 pos 开始逆向查找 str 最后一次出现的位置,如果找到返回最后一次出现的下标,没有找到则返回-1
string str("12345612345");
string str1("6789");
int index = str.rfind(str1); //默认从下标 size()-1 开始逆向查找 str1
cout << index << endl; //index = -1, 找不到 "6789"
string str2("456");
index = str.rfind(str2, 2); //从下标 2 开始逆向查找 str2
cout << index << endl; //index = -1
index = str.rfind(str2, str.size()-1); //从下标 size()-1 开始逆向查找 str2
cout << index << endl; //index = 3
int rfind(const char* s, int pos = npos) const; //字符串对象中从下标pos开始逆向查找指针 s 指向的字符串最后一次出现的位置,如果找到返回最后一次出现的下标,没有找到则返回-1
string str("12345612345");
int index = str.rfind("789"); //默认从 size()-1 开始逆向查找
cout << index << endl; //index = -1, 找不到"789"
index = str.rfind("123", str.size()-1); //从下标 size()-1 开始逆向查找
cout << index << endl; //index = 6
int rfind(const char* s, int pos, int n) const; //字符串对象中从下标pos开始逆向查找指针 s 指向的字符串的前 n 个字符子串最后一次出现的位置,如果找到返回最后一次出现的下标,没有找到则返回-1
string str("12345612345");
int index = str.rfind("6789", str.size()-1, 4); //从下标 size()-1 开始逆向查找 "6789"
cout << index << endl; //index = -1, 找不到 "6789"
index = str.rfind("6789", str.size()-1, 1); //从下标 size()-1 开始逆向查找 "6789" 中的子串 "6"
cout << index << endl; //index = 5
index = str.rfind("123", str.size()-1, 3); //从下标 size()-1 开始逆向查找 "123"
cout << index << endl; //index = 6
int rfind(char c, int pos = npos) const; //字符串对象中从下标pos开始逆向查找字符 c 最后一次出现的位置,如果找到返回最后一次出现的下标,没有找到则返回-1
string str("12345612345");
int index = str.rfind('0');
cout << index << endl; //index = -1, 找不到'0'
index = str.rfind('3', 1); //index = -1,从索引1开始逆向找,找不到'3'
cout << index << endl;
index = str.rfind('3', str.size()-1); //从下标size()-1开始逆向找
cout << index << endl; //index = 8
int find_first_of(const string& str, int pos = 0) const; //字符串对象中从下标pos开始正向查找str中任一个字符之一第一次出现在字符串对象中的位置,如果找到返回这个位置的下标,否则返回-1
string str("12345612345");
string str1("789");
int index = str.find_first_of(str1); //默认从下标 0 开始正向查找
cout << index << endl; //index = -1
string str2("432");
index = str.find_first_of(str2); //默认从下标 0 开始正向查找
cout << index << endl; //index = 1
string str3("12");
index = str.find_first_of(str3, 8); //从下标 8 开始正向查找
cout << index << endl; //index = -1
int find_first_of(const char* s, int pos, int n) const; //字符串对象中从下标pos开始正向查找指针 s 指向的字符串前n个字符中任一个字符之一第一次出现在字符串对象中的位置,如果找到返回这个位置的下标,否则返回-1
string str("12345612345");
int index = str.find_first_of("789", 0, 3); //从下标 0 开始正向查找
cout << index << endl; //index = -1
index = str.find_first_of("432", 0, 2); //从下标 0 开始正向查找
cout << index << endl; //index = 2
index = str.find_first_of("12", 8, 2); //从下标 8 开始正向查找
cout << index << endl; //index = -1
int find_first_of(const char* s, int pos = 0) const; //字符串对象中从下标pos开始正向查找指针 s 指向的字符串中任一个字符之一第一次出现在字符串对象中的位置,如果找到返回这个位置的下标,否则返回-1
string str("12345612345");
int index = str.find_first_of("789"); //默认从下标 0 开始正向查找
cout << index << endl; //index = -1
index = str.find_first_of("432"); //默认从下标 0 开始正向查找
cout << index << endl; //index = 1
index = str.find_first_of("12", 8); //从下标 8 开始正向查找
cout << index << endl; //index = -1
int find_first_of(char c, int pos = 0) const; //字符串对象中从下标pos开始正向查找字符 c 第一次出现在字符串对象中的位置,如果找到返回这个位置的下标,否则返回-1
string str("12345612345");
int index = str.find_first_of('7'); //默认从下标 0 开始正向查找
cout << index << endl; //index = -1
index = str.find_first_of('4'); //默认从下标 0 开始正向查找
cout << index << endl; //index = 3
index = str.find_first_of('1', 8); //从下标 8 开始正向查找
cout << index << endl; //index = -1
int find_first_not_of(const string& str, int pos = 0) const; //字符串对象中从下标pos开始正向查找第一次出现不在str字符串中的字符的位置,如果找到返回这个位置的下标,否则返回-1
string str("11345612344");
string str_given("1234");
int index = str.find_first_not_of(str_given); //默认从下标 0 开始正向查找
cout << index << endl; //index = 4
index = str.find_first_not_of(str_given, 5); //从下标 5 开始正向查找
cout << index << endl; //index = 5
string str1("1234321");
index = str1.find_first_not_of(str_given); //默认从下标 0 开始正向查找
cout << index << endl; //index = -1
int find_first_not_of(const char* s, int pos, int n) const; //字符串对象中从下标pos开始正向查找第一次出现不在指针 s 指向的字符串前n个字符中的字符的位置,如果找到返回这个位置的下标,否则返回-1
string str("11345612344");
int index = str.find_first_not_of("1234", 0, 2); //从下标 0 开始正向查找
cout << index << endl; //index = 2
index = str.find_first_not_of("1234", 5, 4); //从下标 5 开始正向查找
cout << index << endl; //index = 5
string str1("1234321");
index = str1.find_first_not_of("1234", 0, 4); //从下标 0 开始正向查找
cout << index << endl; //index = -1
int find_first_not_of(const char* s, int pos = 0) const; //字符串对象中从下标pos开始正向查找第一次出现不在指针 s 指向的字符串中的字符的位置,如果找到返回这个位置的下标,否则返回-1
string str("11345612344");
int index = str.find_first_not_of("1234"); //默认从下标 0 开始正向查找
cout << index << endl; //index = 4
index = str.find_first_not_of("1234", 5); //从下标 5 开始正向查找
cout << index << endl; //index = 5
string str1("1234321");
index = str1.find_first_not_of("1234"); //默认从下标 0 开始正向查找
cout << index << endl; //index = -1
int find_first_not_of(char c, int pos = 0) const; //字符串对象中从下标pos开始正向查找第一次出现不等于字符 c 的字符的位置,如果找到返回这个位置的下标,否则返回-1
string str("11345612344");
int index = str.find_first_not_of('1'); //默认从下标 0 开始正向查找
cout << index << endl; //index = 2
index = str.find_first_not_of('1', 3); //从下标 3 开始正向查找
cout << index << endl; //index = 3
string str1("1111");
index = str1.find_first_not_of('1'); //默认从下标 0 开始正向查找
cout << index << endl; //index = -1
int find_last_of(const string& str, int pos = npos) const; //字符串对象中从下标pos开始逆向查找str中任一个字符之一第一次出现在字符串对象中的位置,如果找到返回这个位置的下标,否则返回-1
string str("12345612345");
string str1("789");
int index = str.find_last_of(str1); //默认从下标 size()-1 开始逆向查找
cout << index << endl; //index = -1
string str2("432");
index = str.find_last_of(str2); //默认从下标 size()-1 开始逆向查找
cout << index << endl; //index = 9
string str3("45");
index = str.find_last_of(str3, 2); //从下标 2 开始逆向查找
cout << index << endl; //index = -1
int find_last_of(const char* s, int pos, int n) const; //字符串对象中从下标pos开始逆向查找指针 s 指向的字符串前n个字符中任一个字符之一第一次出现在字符串对象中的位置,如果找到返回这个位置的下标,否则返回-1
string str("12345612345");
int index = str.find_last_of("789", str.size()-1, 3); //从下标 str.size()-1 开始逆向查找
cout << index << endl; //index = -1
index = str.find_last_of("234", str.size()-1, 2); //从下标 str.size()-1 开始逆向查找
cout << index << endl; //index = 8
index = str.find_last_of("45", 2, 2); //从下标 2 开始逆向查找
cout << index << endl; //index = -1
int find_last_of(const char* s, int pos = npos) const; //字符串对象中从下标pos开始逆向查找指针 s 指向的字符串中任一个字符之一第一次出现在字符串对象中的位置,如果找到返回这个位置的下标,否则返回-1
string str("12345612345");
int index = str.find_last_of("789"); //默认从下标 str.size()-1 开始逆向查找
cout << index << endl; //index = -1
index = str.find_last_of("432"); //默认从下标 str.size()-1 开始逆向查找
cout << index << endl; //index = 9
index = str.find_last_of("45", 2); //从下标 2 开始逆向查找
cout << index << endl; //index = -1
int find_last_of(char c, int pos = npos) const; //字符串对象中从下标pos开始逆向查找字符 c 第一次出现在字符串对象中的位置,如果找到返回这个位置的下标,否则返回-1
string str("12345612345");
int index = str.find_last_of('7'); //默认从下标 str.size()-1 开始逆向查找
cout << index << endl; //index = -1
index = str.find_last_of('4'); //默认从下标 str.size()-1 开始逆向查找
cout << index << endl; //index = 9
index = str.find_last_of('4', 2); //从下标 2 开始逆向查找
cout << index << endl; //index = -1
int find_last_not_of(const string& str, int pos = npos) const; //字符串对象中从下标pos开始逆向查找第一次出现不在str字符串中的字符的位置,如果找到返回这个位置的下标,否则返回-1
string str("11345612344");
string str_given("3412");
int index = str.find_last_not_of(str_given); //默认从下标 str.size()-1 开始逆向查找
cout << index << endl; //index = 5
index = str.find_last_not_of("1234", 5); //从下标 5 开始逆向查找
cout << index << endl; //index = 5
string str1("1234321");
index = str1.find_last_not_of("1234"); //默认从下标 str.size()-1 开始逆向查找
cout << index << endl; //index = -1
int find_last_not_of(const char* s, int pos, int n) const; //字符串对象中从下标pos开始逆向查找第一次出现不在指针 s 指向的字符串前n个字符中的字符的位置,如果找到返回这个位置的下标,否则返回-1
string str("11345612344");
int index = str.find_last_not_of("3412", str.size()-1, 2); //从下标 str.size()-1 开始逆向查找
cout << index << endl; //index = 7
index = str.find_last_not_of("1234", 5, 4); //从下标 5 开始逆向查找
cout << index << endl; //index = 5
string str1("1234321");
index = str1.find_last_not_of("1234", str.size()-1, 4); //从下标 str.size()-1 开始逆向查找
cout << index << endl; //index = -1
int find_last_not_of(const char* s, int pos = npos) const; //字符串对象中从下标pos开始逆向查找第一次出现不在指针 s 指向的字符串中的字符的位置,如果找到返回这个位置的下标,否则返回-1
string str("11345612344");
int index = str.find_last_not_of("1234"); //默认从下标 str.size()-1 开始逆向查找
cout << index << endl; //index = 5
index = str.find_last_not_of("1234", 5); //从下标 5 开始逆向查找
cout << index << endl; //index = 5
string str1("1234321");
index = str1.find_last_not_of("1234"); //默认从下标 str.size()-1 开始逆向查找
cout << index << endl; //index = -1
int find_last_not_of(char ch, int pos = npos) const; //字符串对象中从下标pos开始逆向查找第一次出现不等于字符 c 的字符的位置,如果找到返回这个位置的下标,否则返回-1
string str("11345612344");
int index = str.find_last_not_of('4'); //默认从下标 str.size()-1 开始逆向向查找
cout << index << endl; //index = 8
index = str.find_last_not_of('4', 3); //从下标 3 开始逆向查找
cout << index << endl; //index = 2
string str1("1111");
index = str1.find_last_not_of('1'); //默认从下标 str.size()-1 开始逆向查找
cout << index << endl; //index = -1
参考C++STL标准库手册