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

来源:互联网 发布:如何做金融投资 知乎 编辑:程序博客网 时间:2024/06/06 07:00

最近看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_tvoid 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;}

结果:
这里写图片描述

1 1
原创粉丝点击