Already asked this question并收到有关C++ STL的答案,但是boost呢?boost :: algorithm :: string :: finder.hpp
这是一个关于boost搜索器的问题。如果你有一个链接到可描述的boost库实现,我将不胜感激它使得为实际应用寻找boost库变得更容易。
我的问题是哪个助手发现最适用于lastIndexOf?
Already asked this question并收到有关C++ STL的答案,但是boost呢?boost :: algorithm :: string :: finder.hpp
这是一个关于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());
你能解释你的用例吗?为什么你需要一个查找器实例,而不是使用标准(或增强)显式函数来查找字符或模式的最后一个索引?如果您只想要一个可用于查找模式的最后一个索引的函数,请使用已发布的解决方案@sftrabbit。 – 2013-03-26 17:45:56
@ViteFalcon是的,我正在寻找一种功能在增强,发现模式的最后一个索引,并选择使用解决方案张贴。 – Mushy 2013-03-26 17:54:53