C++中std::string::find_last_of用法

Posted NearXDU

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++中std::string::find_last_of用法相关的知识,希望对你有一定的参考价值。

最近看boost代码脑洞打开,比如这个std::string::find_last_of


size_t find_last_of (const string& str, size_t pos = npos) const noexcept;

size_t find_last_of (const char* s, size_t pos = npos) const;

size_t find_last_of (const char* s, size_t pos, size_t n) const;

size_t find_last_of (char c, size_t pos = npos) const noexcept;

顾名思义,可以从后往前找匹配的字符,并且,这个字符可以以字串的形式给出,也就是只要匹配参数中字串的任意字符就返回其位置,这对于UNIX风格和Windows风格的文件夹路径处理很方便,例如:

// string::find_last_of
#include <iostream>       // std::cout
#include <string>         // std::string
#include <cstddef>         // std::size_t

void SplitFilename (const std::string& str)

  std::cout << "Splitting: " << str << '\\n';
  std::size_t found = str.find_last_of("/\\\\");
  std::cout << " path: " << str.substr(0,found) << '\\n';
  std::cout << " file: " << str.substr(found+1) << '\\n';


int main ()

  std::string str1 ("/usr/bin/man");
  std::string str2 ("c:\\\\windows\\\\winhelp.exe");

  SplitFilename (str1);
  SplitFilename (str2);

  return 0;

结果:

以上是关于C++中std::string::find_last_of用法的主要内容,如果未能解决你的问题,请参考以下文章