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

  • Home
  • SEARCH
  • 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 847841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:51:06+00:00 2026-05-15T06:51:06+00:00

I have a Red Black tree implemented in c++ . It supports the functionality

  • 0

I have a Red Black tree implemented in c++. It supports the functionality of a STL map. Tree nodes contain keys and the values mapped. I want to write an iterator class for this, but I’m stuck with how to do it. Should I make it an inner class of the Tree class? Can anyone give me some guidelines on how to write it + some resources??

Thank You!!

  • 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-15T06:51:07+00:00Added an answer on May 15, 2026 at 6:51 am

    Sure, read this nice article on writing STL iterators, it might give you the needed overview:

    http://www.drdobbs.com/184401417

    In general, yes, an inner class is good, because the iterator needs access to your implementation specific tree nodes:

    struct container { ...
    public:
      struct iterator {
        // these typedefs are needed if you want to be STL compatible
        typedef std::forward_iterator_tag iterator_category;
        typedef T         value_type;
        typedef T*        pointer;
        typedef T&        reference;
        typedef size_t    size_type;
        typedef ptrdiff_t difference_type;
    
        // the element points to your implementation node
        iterator( element* init = 0 ) : current(init) {}
        T& operator*() { return current->data; } // dereference
        const T& operator*() const { return current->data; }
        iterator& operator++() { // prefix
          if ( current ) current = current->next;
          return *this;
        }
        iterator operator++(int) { // postfix
          iterator temp = *this;
          ++*this;
          return temp;
        }
        bool operator==(const iterator& x) const { return current == x.current; }
        bool operator!=(const iterator& x) const { return current != x.current; }
    
      private:
        // the element points to your implementation node
        element* current;
      }
      ...
    

    The good thing here is that while the iterator is public, the element can still stay private :). And yes, the code above is STL compilant too!

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

Sidebar

Related Questions

I know that the Map container in STL is internally a Red-Black Tree, which
Since C++ STL set/map are implemented as red-black trees, it should be possible to
I have a red-black tree (binary tree, all the leaves are within 2 levels).
I have an order-statistic augmented red black tree. it works for the most part.
i have the following code, taken partly from a Red Black Tree Java implementation.
I have an array that stores the values: var array = [['favorite color'],['black','red']] to
I have been trying to create a red black tree that only implements an
I have a red black tree algorithm which is working fine. When a node
Suppose I have a binary search tree which, initially, satisfies all of the red-black
Since black red tree is a binary search tree I have decided to use

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.