我需要一种方法来帮助我在另一个子字符串中找到一个字符串,或者换句话说,在另一个字符串的子范围内找到一个字符串。此外,我需要以相反的顺序找到它,因为我知道我要查找的字符串接近用作“干草堆”的子字符串的末尾。
让我们假设以下一段代码,rfind_in_substr
我要求的方法在哪里:
std::string sample("An example with the example word example trice");
// substring "ample with the example wo"
std::size_t substr_beg = 5;
std::size_t substr_size = 24;
// (1)
std::size_t pos = rfind_in_substr(sample, substr_beg,
substr_size, "example");
// pos == 20, because its the index of the start of the second
// "example" word inside the main string.
当然,第 (1) 行可以替换为:
std::size_t pos = substr_beg + sample.substr
(substr_beg, substr_size).rfind("example");
但这意味着子字符串的不必要副本。是否有任何方法或 C++/boost 方法可以帮助我做到这一点?
我在看boost::algorithm::string
图书馆,但我什么也没找到(我已经理解了)。我知道 C++17 有这个std::string_view
类,那将是完美的,但我使用的是 C++14。