Already asked this question并收到了有关 c++ STL 的答案,但是 boost 呢?
这是一个关于 boost Finders 的问题。如果您有可描述的 boost 库实现的链接,我将不胜感激,因为它可以让您更轻松地寻找实际应用程序的 boost 库。
我的问题是哪个 boost 查找器最适用于 lastIndexOf ?
最佳答案
首先,如果您要搜索子字符串最后一次出现的位置,最简单的选择是使用 std::string::rfind
:
std::string str = "Hello, World!";
int index = str.rfind("o");
如果您需要使用 Boost,因为您希望它在通用范围内工作,请使用 boost::algorithm::find_last
。它需要两个范围。在第一个范围中搜索第二个范围。
std::string str = "Hello, World!";
iterator_range<std::string::iterator> it = find_last(str, "o");
int index = std::distance(str.begin(), it.begin());
如果您确实想使用查找器,那么您似乎正在寻找boost::algorithm::last_finder
。查找器返回一个函数对象,该对象采用两个迭代器作为其参数。该函数返回一个 iterator_range
您可以像这样使用它:
auto finder = last_finder("o");
std::string str = "Hello, World!";
iterator_range<std::string::iterator> it = finder(str.begin(), str.end());
int index = std::distance(str.begin(), it.begin());
关于c++ - boost::算法::字符串::finder.hpp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15643576/