📜  C++中的std :: 字符串:: find_last_not_of(1)

📅  最后修改于: 2023-12-03 15:14:03.144000             🧑  作者: Mango

C++中的std::字符串::find_last_not_of

简介

在C++中,std::字符串(string)是一个非常常用的容器类,它能够更方便的处理字符类型的数据。std::字符串::find_last_not_of是它的一个成员函数,用于查找一个字符串中不属于另一个指定字符串中的最后一个字符的位置。

语法如下:

size_t find_last_not_of (const string& str, size_t pos = npos) const noexcept;
size_t find_last_not_of (const char* s, size_t pos, size_t n) const;
size_t find_last_not_of (const char* s, size_t pos = npos) const;
size_t find_last_not_of (char c, size_t pos = npos) const noexcept;
参数
  • str: 指定的字符串,用于查找目标字符串中不属于的字符。
  • s: 指定的字符数组,用于查找目标字符串中不属于的字符。
  • pos: 开始查找位置,默认为npos(字符串的结尾)。
  • n: 字符数组s中的字符数。
  • c: 指定的字符,用于查找目标字符串中不属于的字符。
返回值

如果查找成功,则返回最后一个不属于指定字符串的字符位置的下标,否则返回std::字符串::npos。

示例
#include <iostream>
#include <string>
using namespace std;

int main () {
  string s ("example string");
  int found = s.find_last_not_of("gni"); // 查找最后一个不属于"gni"中的字符的下标位置
  if (found!=string::npos)
    cout << "最后一个不属于'gni'中的字符的位置为:" << found << '\n';
  else
    cout << "未找到不属于'gni'中的字符!\n";
  return 0;
}

输出结果:

最后一个不属于'gni'中的字符的位置为:12
总结

std::字符串::find_last_not_of是一个非常实用的函数,它可以用于查找目标字符串中不属于另一个字符串中的最后一个字符的位置。程序员在处理字符串时,可以灵活地使用该函数来实现自己的需求。