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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:02:18+00:00 2026-05-16T18:02:18+00:00

Is there any existing iterator implementation (perhaps in boost) which implement some sort of

  • 0

Is there any existing iterator implementation (perhaps in boost) which implement some sort of flattening iterator?

For example:

unordered_set<vector<int> > s;

s.insert(vector<int>());
s.insert({1,2,3,4,5});
s.insert({6,7,8});
s.insert({9,10,11,12});

flattening_iterator<unordered_set<vector<int> >::iterator> it( ... ), end( ... );
for(; it != end; ++it)
{
    cout << *it << endl;
}
//would print the numbers 1 through 12
  • 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-16T18:02:18+00:00Added an answer on May 16, 2026 at 6:02 pm

    I don’t know of any implementation in a major library, but it looked like an interesting problem so I wrote a basic implementation. I’ve only tested it with the test case I present here, so I don’t recommend using it without further testing.

    The problem is a bit trickier than it looks because some of the “inner” containers may be empty and you have to skip over them. This means that advancing the flattening_iterator by one position may actually advance the iterator into the “outer” container by more than one position. Because of this, the flattening_iterator needs to know where the end of the outer range is so that it knows when it needs to stop.

    This implementation is a forward iterator. A bidirectional iterator would also need to keep track of the beginning of the outer range. The flatten function templates are used to make constructing flattening_iterators a bit easier.

    #include <iterator>
    
    // A forward iterator that "flattens" a container of containers.  For example,
    // a vector<vector<int>> containing { { 1, 2, 3 }, { 4, 5, 6 } } is iterated as
    // a single range, { 1, 2, 3, 4, 5, 6 }.
    template <typename OuterIterator>
    class flattening_iterator
    {
    public:
    
        typedef OuterIterator                                outer_iterator;
        typedef typename OuterIterator::value_type::iterator inner_iterator;
    
        typedef std::forward_iterator_tag                iterator_category;
        typedef typename inner_iterator::value_type      value_type;
        typedef typename inner_iterator::difference_type difference_type;
        typedef typename inner_iterator::pointer         pointer;
        typedef typename inner_iterator::reference       reference;
    
        flattening_iterator() { }
        flattening_iterator(outer_iterator it) : outer_it_(it), outer_end_(it) { }
        flattening_iterator(outer_iterator it, outer_iterator end) 
            : outer_it_(it), 
              outer_end_(end)
        { 
            if (outer_it_ == outer_end_) { return; }
    
            inner_it_ = outer_it_->begin();
            advance_past_empty_inner_containers();
        }
    
        reference operator*()  const { return *inner_it_;  }
        pointer   operator->() const { return &*inner_it_; }
    
        flattening_iterator& operator++()
        {
            ++inner_it_;
            if (inner_it_ == outer_it_->end())
                advance_past_empty_inner_containers();
            return *this;
        }
    
        flattening_iterator operator++(int)
        {
            flattening_iterator it(*this);
            ++*this;
            return it;
        }
    
        friend bool operator==(const flattening_iterator& a, 
                               const flattening_iterator& b)
        {
            if (a.outer_it_ != b.outer_it_)
                return false;
    
            if (a.outer_it_ != a.outer_end_ && 
                b.outer_it_ != b.outer_end_ &&
                a.inner_it_ != b.inner_it_)
                return false;
    
            return true;
        }
    
        friend bool operator!=(const flattening_iterator& a,
                               const flattening_iterator& b)
        {
            return !(a == b);
        }
    
    private:
    
        void advance_past_empty_inner_containers()
        {
            while (outer_it_ != outer_end_ && inner_it_ == outer_it_->end())
            {
                ++outer_it_;
                if (outer_it_ != outer_end_) 
                    inner_it_ = outer_it_->begin();
            }
        }
    
        outer_iterator outer_it_;
        outer_iterator outer_end_;
        inner_iterator inner_it_;
    };
    
    template <typename Iterator>
    flattening_iterator<Iterator> flatten(Iterator it)
    {
        return flattening_iterator<Iterator>(it, it);
    }
    
    template <typename Iterator>
    flattening_iterator<Iterator> flatten(Iterator first, Iterator last)
    {
        return flattening_iterator<Iterator>(first, last);
    }
    

    The following is a minimal test stub:

    #include <algorithm>
    #include <iostream>
    #include <set>
    #include <vector>
    
    int main()
    {
        // Generate some test data:  it looks like this:
        // { { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 } }
        std::vector<std::vector<int>> v(3);
        int i(0);
        for (auto it(v.begin()); it != v.end(); ++it)
        {
            it->push_back(i++); it->push_back(i++);
            it->push_back(i++); it->push_back(i++);
        }
    
        // Flatten the data and print all the elements:
        for (auto it(flatten(v.begin(), v.end())); it != v.end(); ++it)
        {
            std::cout << *it << ", ";
        }
        std::cout << "\n";
    
        // Or, since the standard library algorithms are awesome:
        std::copy(flatten(v.begin(), v.end()), flatten(v.end()), 
                  std::ostream_iterator<int>(std::cout, ", "));
    }
    

    Like I said at the beginning, I haven’t tested this thoroughly. Let me know if you find any bugs and I’ll be happy to correct them.

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

Sidebar

Related Questions

Are there any existing add-ons which would provide the functionality Upload image from local
Are there any pre-existing solutions out there which would extend the built in SQL
is there any existing function in jquery library with the help of which I
Is there any existing Bentley-Ottmann Algorithm Implementation/library in C# or Java?
Is there any existing implementation for query expansion in Perl? By query expansion I
Aside from writing custom code are there any existing features in .NET which allows
Is there any existing JSON parser which can be used from VB6? I could
Is there any existing wiki support direct drawing function? Perhaps it is not a
Is there any existing mechanism by which I can add an event listener to
Is there any existing implementation that compute this in Perl? nCk (n choose k),

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.