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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:56:26+00:00 2026-06-12T23:56:26+00:00

I want to implement a cyclic list based on std::list. I want to profit

  • 0

I want to implement a cyclic list based on std::list. I want to profit from the benfits of the list but add one specific feature: its iterator operators ++ and — should hop over the edges and operations (insert/erase) must not invalidate existing iterators. My skills in handling templates are weak and to understand the std containers is an impossible act for me. Hence i need your help. By now im not that far :D. Sorry but even the numerous posts dont help me any further.

EDIT:
Well after a lot of work, a steeeep learnig curve, the failed approach to inherit from std::list::iterator, a short-term depression and a abasing return to your approaches (yes, you all were right) I finally made it. Inspired by all your contibutions, I can now post what I did the last … about 12 hours 😀 Basicly what you suggested, but with nice little operators.

#pragma once
#include <list>
using std::list;

template<class T>
class cyclic_iterator;


template<class T>
class cyclicList : public list<T>
{
public:
  typedef cyclic_iterator<T> cyclic_iterator;

  cyclic_iterator cycbegin()
  {// not the purpose, but needed for instanziation
    return cyclic_iterator( *this, this->begin());
  }

  cyclic_iterator cycend()
  {// not the purpose, but needed for instanziation
    return cyclic_iterator( *this, this->end());
  }
};




template<class T>
class cyclic_iterator
{
  public:
  // To hop over edges need to know the container
  cyclic_iterator(){}
  cyclic_iterator(typename list<T>::iterator i)
    : mIter(i){}
  cyclic_iterator(list<T> &c)
    : mContainer(&c){}
  cyclic_iterator(list<T> &c, typename list<T>::iterator i)
    : mContainer(&c), mIter(i){}

  cyclic_iterator<T>& operator=(typename list<T>::iterator i)
  {// assign an interator
    mIter = i;
    return *this;
  }

  cyclic_iterator<T>& operator=(list<T> &c)
  {// assign a container
    mContainer = &c;
    return *this;
  } 

  bool operator==(const cyclic_iterator<T>& rVal) const
  {// check for equality
    return (this->mIter == rVal.mIter && this->mContainer == rVal.mContainer) ? true : false;
  } 

  bool operator!=(const cyclic_iterator<T>& rVal) const
  {//  check for inequality
    return !(this->operator==(rVal));
  } 

  cyclic_iterator<T>& operator++()
  {// preincrement
    ++mIter;
    if (mIter == mContainer->end())
      { mIter = mContainer->begin(); }
    return *this;
  }

  cyclic_iterator<T> operator++(int)
  { // postincrement
    cyclic_iterator<T> tmp = *this;
    ++*this;
    return tmp;
  }

  cyclic_iterator<T>& operator--()
  {// predecrement
    if (mIter == mContainer->begin())
      mIter = --mContainer->end();
    else --mIter;
    return *this;
  }

  cyclic_iterator<T> operator--(int)
  {// postdecrement
    cyclic_iterator<T> tmp = *this;
    --*this;
    return tmp;
  }

  cyclic_iterator<T>& operator+=(int j)
  {// hop j nodes forward
    for (int i = 0; i < j; ++i)
      ++(*this);
    return *this;
  }

  cyclic_iterator<T>& operator-=(int j)
  {// hop j nodes backwards
    for (int i = 0; i < j; ++i)
      --(*this);
    return *this;
  }

  T& operator*()
  {
    return *mIter;
  }

  typename list<T>::iterator & getStdIterator()
  {
    return mIter;
  }

private:  
  list<T>*          mContainer;
  typename list<T>::iterator mIter;

};
  • 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-12T23:56:28+00:00Added an answer on June 12, 2026 at 11:56 pm

    Can’t you just make a different iterator type?

    #include <iterator>
    #include <list>
    
    template <typename T, typename Alloc>
    struct cyclic_iterator
    : std::iterator<typename std::list<T, Alloc>::iterator::iterator_category, T>
    {
        typedef std::list<T, Alloc> list_type;
    
        cyclic_iterator & operator++()
        {
            ++iter;
            if (iter == container.end()) { iter = container.begin(); }
            return *this;
        }
    
        T & operator*() { return *iter; }
    
        cyclic_iterator(typename list_type::iterator it, list_type & l)
        : iter(it)
        , container(l)
        {
            if (it == container.end()) { it = container.begin(); }
        }
    
        // everything else
    
    private:
        typename list_type::iterator iter;
        list_type & container;
    };
    

    With a helper:

    template <typename List>
    cyclic_iterator<typename List::value_type, typename List::allocator_type>
    make_cyclic_iterator(typename List::iterator it, List & l)
    {
        return cyclic_iterator<typename List::value_type, typename List::allocator_type>(it, l);
    }
    

    Usage:

    // goes round and round forever
    
    for (auto ci = make_cyclic_iterator(mylist.begin(), mylist); ; ++ci)
    {
        std::cout << *ci << std::endl;
    }
    

    (With a few modifications, this code could be made to work on any container that exposes begin/end iterators.)

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

Sidebar

Related Questions

I want to implement caching of a list pulled from the db. I've had
Want to implement this feature but how? We know that stack overflow has been
I want implement in my software solution an VBA editor but in c# 3.0.
I want to implement a list of ViewPagers. Every item of a ListView is
I want to implement the shopping cart by using multidimensional session array but don't
I want to implement a Next feature in my app. The button appears on
I want implement forward geocoding in my app. But I am not able to
I want implement a program that can do a a Vision-based Page Segmentation. I
I want implement a bump feature for topics. Once a topic is bumped, it
want to implement poor man's ssl only to encrypt certain fields of only one

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.