您现在的位置:首页 > 教案模板 > 正文

first对其输入采用非const引用?(2)

2019-09-21 22:02 网络整理 教案网

本文地址:IT屋 » 为什么boost :: find_first对其输入采用非const引用?

问 题

boost string find_std string find_string find

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 , 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"));

but history suggests that this response will be temporary. by 1940 the divorce rate was higher than before the depression, as if a pent-up demand was finally being satisfied. the depression destroyed the inner life of many married couples, but it was years before they could afford to file for divorce.。restrictions on temporary tablespacesthe data stored in temporary tablespaces persists only for the duration of a session. therefore, only a subset of the create tablespace clauses are relevant for temporary tablespaces. the only clauses you can specify for a temporary tablespace are the tempfile clause, the tablespace_group_clause, and the extent_management_clause.。the java-style iterators are new in qt 4 and are the standard ones used in qt applications. they are more convenient to use than the stl-style iterators, at the price of being slightly less efficient. their api is modelled on java's iterator classes.。

本文地址:IT屋 » Why does boost::find_first take a non-const reference to its input?

官方微信

扫一扫关注IT屋

微信公众号搜索 “ IT屋 ” ,选择关注与百万开发者在一起