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 8090291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:40:14+00:00 2026-06-05T19:40:14+00:00

I would like to create and hold on to an iterator_range. The range is

  • 0

I would like to create and hold on to an iterator_range. The range is constructed based on a predicate (for this example, I look for even numbers).

I can do this, but it seems I must make a copy elements from the underlying vector that is being iterated.

Please look for the comments marked “>>>” in the sample below.

Is there a way to create the iterator_range and NOT have to create a duplicate of entries from the original vector?

I looked, and have not seen, an answer to this particular situation.

#include <vector>
#include <iostream>

#include <boost/bind.hpp>
#include <boost/range.hpp>
#include <boost/foreach.hpp>
#include <boost/iterator/filter_iterator.hpp>
#include <boost/range/iterator_range.hpp>

using namespace std; 
using namespace boost; 

typedef boost::iterator_range<vector<int>::iterator> int_range;

template< class Range, class Pred >
boost::iterator_range< boost::filter_iterator< Pred, typename boost::range_iterator<Range>::type > > 
make_filter_range( Range& rng, Pred pred ) {
    return boost::make_iterator_range( 
     boost::make_filter_iterator(pred, boost::begin(rng), boost::end(rng)), 
     boost::make_filter_iterator(pred, boost::end(rng), boost::end(rng)) );
}

// This is the predicate evaluation function.
bool IsEvenFilter(int val) { return val % 2 == 0; }

void TestMakeIteratorRange()
{
    std::vector<int> vals;
    vals.push_back(1);
    vals.push_back(4);
    vals.push_back(7);
    vals.push_back(11);
    vals.push_back(16);
    vals.push_back(19);
    vals.push_back(28);

    //>>> The following int_range line does not compile.  Why?
    //>>> How can I return an int_range?
    //>>> int_range intRange = make_filter_range( vals, boost::bind(&IsEvenFilter, _1));

    //>>> The following WILL work, but it forces a second copy of elements from vals.
    std::vector<int> v2 = boost::copy_range< std::vector<int> >( 
        make_filter_range( vals, boost::bind(&IsEvenFilter, _1)));

    int_range intRange = int_range(v2);

    // Dump out contents
    BOOST_FOREACH(int &val, intRange)
    {
        cout << " " << val;
    }
    cout << endl;
}

void main()
{
    TestMakeIteratorRange();
}
  • 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-05T19:40:15+00:00Added an answer on June 5, 2026 at 7:40 pm
    int_range intRange = make_filter_range( vals, boost::bind(&IsEvenFilter, _1));
    

    You have to store the type returned by make_filter_range. Which is not int_range.

    This is incidentally why auto exists (in C++11); so that you don’t have to type that return value if you want to store what the function returns. If you don’t have access to C++11 auto, use BOOST_AUTO instead.

    If you can’t use that for some reason, you can also use any_range. Which, as the name suggests, can store any range for a specific type.

    Also, consider using the proper Boost range-adapters, like boost::adaptors::filtered instead of make_filter_iterator.

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

Sidebar

Related Questions

I would like to create a class whose methods can be called from multiple
I would like to create an animation to demonstrate LDPC coding which is based
I would like to create a view from the following table. This table contains
I would like to know how to create a structure to hold three lists
i would like create a array of structure which have a dynamic array :
I would like to create a flash presentation for a web page. It would
I would like to create a COM object that runs in an out of
I would like to create a 'New Document' dialog similar to the Office 2007
I would like to create a UIBarButtonItem on my iPhone application which has two
I would like to create a stored procedure with parameters that indicate which fields

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.