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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:06:54+00:00 2026-05-23T19:06:54+00:00

Here’s a fairly normal encapsulation of an STL container which allows the user of

  • 0

Here’s a fairly normal encapsulation of an STL container which allows the user of Cfoo to iterate the container without allowing changes to the innards.

#include <vector>

class Cfoo
{
public:
    class Cbar
    {
        /* contents of Cbar */
    };
    typedef std::vector<Cbar> TbarVector;
    typedef TbarVector::const_iterator const_iterator;     
public:
    const_iterator begin() const { return( barVector_.begin() ); }
    const_iterator end() const { return( barVector_.end() ); }
private:
    TbarVector barVector_;
};

So far, so good. We can iterate the container like this:

Cfoo myFoo;
for (Cfoo::const_iterator it = myFoo.begin(); it != myFoo.end(); ++it)
{
   it->DoSomething();
}

Now I want to replace the std::vector with say a nested std::vector:

public:
    typedef std::vector<Cbar> TbarVectorInner;
    typedef std::vector<TbarVectorInner> TbarVectorOuter;

private:
    TbarVectorOuter barContainer_;

But I want to be able to iterate over all the instances of Cbar in the same way as before, exposing a const_iterator, and a begin()const and an end()const method.

I’m not clear how to do that, though I suspect it involves writing a custom iterator. Any thoughts?

  • 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-23T19:06:54+00:00Added an answer on May 23, 2026 at 7:06 pm

    None of the standard iterators are able to iterate over more than a single container, so your assumption is correct – you’ll have to write a custom iterator.

    It is possible to do this in a generic fashion, if you have an intermediate iterator that returns pairs of (begin,end) iterators to the inner containers.

    Some untested code to get started:

    template<typename T, typename OuterIterator, typename InnerIterator>
    class Iterator2d : std::Iterator
    {
    public:
        Iterator2d(OuterIterator begin, OuterIterator end) : m_begin(begin), m_end(end), m_currentOuter(begin)    {
            if (m_currentOuter != m_end)
                 m_currentInner = m_begin->first;
            Normalize();
        }
        Iterator2d & operator++()
        {
            if (m_currentOuter != m_end)
            {
                ++m_currentInner;
                Normalize();
            }
            return *this;
        }
        T & operator*()
        {
            return *m_currentInner;
        }
    private:
        void Normalize()
        {
            while (m_currentOuter != m_end && m_currentInner == m_currentOuter->second)
            {
                ++m_currentOuter;
                if (m_currentOuter != m_end)
                    m_currentInner = m_currentOuter->first;
            }
        }
    
        OuterIterator m_begin;
        OuterIterator m_end;
        OuterIterator m_currentOuter;
        InnerIterator m_currentInner;
    };
    

    This is just a start, I may come back to finish it – or not, depending on this implementation already covers the same ground.

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

Sidebar

Related Questions

Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`
Here is what is supposed to happen: The moment the user chooses an option
Here's the flow that I am trying to achieve: 1) User uploads an audio
Here a simple question : What do you think of code which use try
Here is my code, which takes two version identifiers in the form 1, 5,
Here is the scenario: I'm writing an app that will watch for any changes
Here's my situation: I have a Data Template set up which contains a ToggleButton
Here is a part of my code which sends an email: servidor = smtplib.SMTP()
Here i create table in database dynamically. User enters name as his wish and
Here is my scenario: I have a WPF application which I am delivering via

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.