为什么boost :: find_first对其输入采用非const引用? [英] Why does boost::find_first take a non-const reference to its input?
问题描述
Boost的 find_first
算法是一个等价的C的 strstr()
,但为什么haystack—搜索空间作为非常量引用传递?匹配范围在单独的 iterator_range
对象中返回,因此不是通过引用输出的问题。
它阻止使用由 make_iterator_range 。
const std :: string str(haystack);
const std :: string findstr(stack);
boost :: sub_range< const std :: string> match = boost :: algorithm :: find_first(
boost :: make_iterator_range(str),
boost :: make_iterator_range(findstr));相反,必须显式地创建表示源范围的局部变量:
$ b < b
const std :: string str(haystack);
const std :: string findstr(stack);
boost :: sub_range< const std :: string> haystack = boost :: make_iterator_range(str);
boost :: sub_range< const std :: string> match = boost :: algorithm :: find_first(
haystack,
boost :: make_iterator_range(findstr));
(这同样适用于 boost / algorithm / string / find.hpp ,ie 。 find , ifind_first
, find_last
, ifind_last
, find_nth
, ifind_nth
code>, find_tail
& find_token
)。
解决方案这是为了确保返回的范围在调用 find_first
后仍然有效。
虽然上面的初始情况会很好,但下面的结果会导致指向一个被破坏的临时字符串的匹配 c>:
boost :: sub_range< const std :: string>匹配= boost :: algorithm :: find_first(
boost :: make_iterator_range(std :: string(haystack),
boost :: make_iterator_range(std :: string(stack));
haystack是非常量的要求阻止它绑定到临时对象(rvalue)在 find_first
的返回并且使匹配
的迭代器无效。
Boost's find_first
algorithm is a souped-up equivalent of C's strstr()
, but why does the haystack — the search space — get passed in as a non-const reference? The matching range is returned in a separate iterator_range
object, so it's not a matter of output-by-reference.
It prevents invocation with a temporary range created by make_iterator_range.
const std::string str("haystack");
const std::string findstr("stack");
boost::sub_range<const std::string> match = boost::algorithm::find_first(
boost::make_iterator_range(str),
boost::make_iterator_range(findstr));
Instead, a local variable representing the source range must be created explicitly:
const std::string str("haystack");
const std::string findstr("stack");
boost::sub_range<const std::string> haystack = boost::make_iterator_range(str);
boost::sub_range<const std::string> match = boost::algorithm::find_first(
haystack,
boost::make_iterator_range(findstr));
(This applies equally to the other functions in boost/algorithm/string/find.hpp, ie. find
, ifind_first
, find_last
, ifind_last
, find_nth
, ifind_nth
, find_head
, find_tail
& find_token
).
解决方案 It's to ensure that the returned range is still valid after the call to find_first
.
While the initial case above would be fine, the following would result in a match
that points to a destroyed temporary string:
boost::sub_range<const std::string> match = boost::algorithm::find_first(
boost::make_iterator_range(std::string("haystack"),
boost::make_iterator_range(std::string("stack"));
The requirement that the haystack be non-const prevents it binding to a temporary object (rvalue) which is destroyed upon find_first
's return and invalidates the match
's iterators.
这篇关于为什么boost :: find_first对其输入采用非const引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!