Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8862791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:48:49+00:00 2026-06-14T15:48:49+00:00

Boost’s find_first algorithm is a souped-up equivalent of C’s strstr() , but why does

  • 0

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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:48:50+00:00Added an answer on June 14, 2026 at 3:48 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does boost chrono provides time stamp with nanoseconds resolution?? If yes how to get
Using Boost Program Options, how do you get the string equivalent of argv[0]?
Does Boost, or anything else, contain a container will act like a shared pointer
Boost has both enable_if and disable_if , but C++0x seems to be missing the
boost::tuple has a get() member function used like this: tuple<int, string, string> t(5, foo,
Boost rocks, it is great and extremely powerful, but I hate it everytime I
STL/Boost cover all the low level stuff. But what about the higher level concepts?
Boost does not provide single-threaded event-driven model, such that was widespread on Unix before
boost::lexical_cast is a great tool but in my application I ran into a limitation
boost::shared_ptr really bothers me. Certainly, I understand the utility of such a thing, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.