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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:19:59+00:00 2026-05-25T03:19:59+00:00

I am using std::transform with an std::back_inserter to append elements to an std::deque .

  • 0

I am using std::transform with an std::back_inserter to append elements to an std::deque. Now the transformation may fail and will return a invalid object (say an uninitialized boost::optional or a null pointer) in some cases. I would like to filter out the invalid objects from getting appended.

I thought about using boost::filter_iterator, but not sure how to present the end() parameter of the filtered range.

The documentation of boost::filter_iterator suggests that output filtering is possible. Should I just specialize operator == for std::back_insert_iterator in this case to always return false?

In addition to this, if I want to append values of initialized boost::optional or pointers, can I chain boost::filter_iterator and boost::indirect_iterator?

I am trying to avoid rolling out my own transform_valid function that takes an optional extractor function.

Is it even possible to use filter_iterator as an output iterator?

  • 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-05-25T03:20:00+00:00Added an answer on May 25, 2026 at 3:20 am

    I suggest using boost range (algorithms & adaptors) for ease of use, you’d write:

    boost::copy(
        data | transformed(makeT) | filtered(validate) /* | indirected */, 
        std::back_inserter(queue));
    

    Here is a complete working example of that:

    #include <boost/range.hpp>
    #include <boost/range/adaptors.hpp>
    #include <boost/range/algorithm.hpp>
    #include <boost/optional.hpp>
    
    #include <vector>
    #include <deque>
    
    typedef boost::optional<int> T;
    typedef std::deque<T> Q;
    
    static T makeT(int i)
    {
        if (i%2) return T();
        else     return i;
    }
    
    static bool validate(const T& optional) 
    { 
        return (bool) optional; // select the optional that had a value set
    }
    
    int main()
    {
        static const int data[] =  { 1,2,3,4,5,6,7,8,9 };
    
        Q q;
    
        using boost::adaptors::filtered;
        using boost::adaptors::transformed;
    
        // note how Boost Range elegantly supports an int[] as an input range
        boost::copy(data | transformed(makeT) | filtered(validate), std::back_inserter(q));
    
        // demo output: 2, 4, 6, 8 printed
        for (Q::const_iterator it=q.begin(); it!=q.end(); ++it)
        {
            std::cout << (*it? "set" : "unset") << "\t" << it->get_value_or(0) << std::endl;
        }
    
        return 0;
    }
    

    Update

    With a little help from this answer: Use boost::optional together with boost::adaptors::indirected

    I now include an elegant demonstration of using the indirected range adaptor as well for immediate output of the queue (dereferencing the optionals):

    Note that for (smart) pointer types there would obviously be no need to provide the pointee<> specialisation. I reckon this is by design: optional<> is not, and does not model, a pointer

    #include <boost/range.hpp>
    #include <boost/range/adaptors.hpp>
    #include <boost/range/algorithm.hpp>
    
    #include <boost/optional.hpp>
    
    namespace boost {
        template<typename P> struct pointee<optional<P> > {
            typedef typename optional<P>::value_type type;
        };
    }
    
    typedef boost::optional<int> T;
    
    static T    makeT(int i)                { return i%2?  T() : i; }
    static bool validate(const T& optional) { return (bool) optional; }
    
    int main() {
        using namespace boost::adaptors;
    
        static int data[] =  { 1,2,3,4,5,6,7,8,9 };
        boost::copy(data | transformed(makeT) 
                         | filtered(validate) 
                         | indirected, 
                         std::ostream_iterator<int>(std::cout, ", "));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now I am using std::pair to represent a 2d point in c++. However,
I'm using std::map to store a lot of elements (pairs of elements) and I
Up until now I have been using std::string in my C++ applications for embedded
I'm using transform algorithm and std::toupper to achieve this, but can this be done
std::sort swaps elements by using std::swap , which in turn uses the copy constructor
Instead of using std::vector<Object> ObjectArray; I would like it to be MyArray<Object> ObjectArray; with
I was using std::hash_map<char*,T> and somehow managed to make it work but have now
I'm currently using std::ofstream as follows: std::ofstream outFile; outFile.open(output_file); Then I attempt to pass
In C++ using std::list , removing a member is a simple matter of erasing
I'm using std::getline() to read lines from an std::istream-derived class, how can I move

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.