473,761 Members | 2,455 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

'Looping' an std::list

Hi, I would like to change the behavior of the std::list so that if I
set an iterator to the last element and increment it I would like it to
point to the first element, and vice vesa. That is, i would like to use
it as a circular buffer.

Is this possble? Given that the linked list is probably just a set of
nodes and 'next' pointers it doesn't seem unreasonable (just set the
last pointer to point at the start) but I suspect the std::list is to
safe/robust to allow me to abuse it in this way...

Be great if someone could prove me wrong :-D Otherwise is there an
STL/Boost container giving this behaviour? I think i could do it with
boost::graph but seems a bit of a heavyweight solution.

Thanks,

David

May 27 '06 #1
2 1842
In article <11************ **********@j55g 2000cwa.googleg roups.com>,
"esuvs" <da***@david-williams.info> wrote:
Hi, I would like to change the behavior of the std::list so that if I
set an iterator to the last element and increment it I would like it to
point to the first element, and vice vesa. That is, i would like to use
it as a circular buffer.

Is this possble? Given that the linked list is probably just a set of
nodes and 'next' pointers it doesn't seem unreasonable (just set the
last pointer to point at the start) but I suspect the std::list is to
safe/robust to allow me to abuse it in this way...

Be great if someone could prove me wrong :-D Otherwise is there an
STL/Boost container giving this behaviour? I think i could do it with
boost::graph but seems a bit of a heavyweight solution.


I posted the below back in March... (Pieter Pareit seems to have posted
something very much like it back in Jan of 2002.)

You can use the circular_iterat or on any of the standard containers. The
code below does need some fleshing out (for eg: post-increment and
decrement, operator->() and const dereference operators) but the below
is enough for most uses.
* *template < typename Container >
class circular_iterat or_t : public std::iterator<
* * * * * * * *typename Container::iter ator::iterator_ category,
* * * * * * * *typename Container::iter ator::value_typ e >
{
* *Container& rep;
* *typename Container::iter ator loc;
public:
* *explicit circular_iterat or_t( Container& container ):
* * * * * * * *rep( container ), loc( container.begin () ) { }
* *circular_itera tor_t( Container& container,
* * * * * * * *typename Container::iter ator it ):
* * * * * * * *rep( container ), loc( it ) { }
* *typename Container::valu e_type& operator*() {
* * * return *loc;
* *}
* *circular_itera tor_t& operator--() {
* * * if ( loc == rep.begin() )
* * * * *loc = --rep.end();
* * * else
* * * * *--loc;
* * * return *this;
* *}
* *circular_itera tor_t& operator++() {
* * * ++loc;
* * * if ( loc == rep.end() )
* * * * *loc = rep.begin();
* * * return *this;
* *}
* *friend bool operator==( const circular_iterat or_t& lhs,
* * * * * * * *const circular_iterat or_t& rhs ) {
* * * return lhs.rep == rhs.rep && lhs.loc == rhs.loc;
* *}
};

* *template < typename Container >
bool operator!=( const circular_iterat or_t<Container> & lhs,
* * * * * * * *const circular_iterat or_t<Container> & rhs ) {
* *return !( lhs == rhs );

}

* *template < typename Container >
circular_iterat or_t<Container> circular_iterat or( Container& container )
{
* *return circular_iterat or_t<Container> ( container );
}

* *template < typename Container >
circular_iterat or_t<Container> circular_iterat or( Container& container,
typename Container::iter ator it ) {
* *return circular_iterat or_t<Container> ( container, it );
}
May 27 '06 #2
That's excellent, thanks. I was wondering if I should implement my own
container but yours is a more elegant solution.

David

May 28 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2847
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is over 1K in size. What methods of the std stl list class must Ioverride in order for this to work?
5
1787
by: Eric Lilja | last post by:
Hello, consider this complete program (sorry, it's not minimal but I hope it's readable at least): #include <algorithm> #include <iostream> #include <vector> class Row { public:
44
3876
by: Josh Mcfarlane | last post by:
Just out of curiosity: When would using std::list be more efficient / effective than using other containers such as vector, deque, etc? As far as I'm aware, list doesn't appear to be specialized for anything. Thanks, Josh McFarlane
7
2975
by: alex221 | last post by:
In need to implement a tree structure in which every node has arbitrary number of children the following code has come into mind: using std::list; template < class Contents class Tree_node{ Contents cn; list < BTree_node < Contents children; ........
8
3422
by: Spoon | last post by:
Hello, Could someone explain why the following code is illegal? (I'm trying to use a list of (C-style) arrays.) #include <list> typedef std::list < int foo_t; int main() { int v = { 12, 34 };
0
1730
by: Javier | last post by:
Hi all, I have this code: class A { std::list<Bm_observadores; void function() {
3
2528
by: Ray D. | last post by:
Hey all, I'm trying to pass a list into a function to edit it but when I compile using g++ I continue to get the following error: maintainNeighbors.cpp:104: error: invalid initialization of non-const reference of type 'std::list<HostID, std::allocator<HostID&' from a temporary of type 'std::list<HostID, std::allocator<HostID*' helpers.cpp:99: error: in passing argument 1 of `void
12
2708
by: isliguezze | last post by:
template <class T> class List { public: List(); List(const List&); List(int, const T&); void push_back(const T &); void push_front(const T &); void pop_back();
17
4192
by: Isliguezze | last post by:
Does anybody know how to make a wrapper for that iterator? Here's my wrapper class for std::list: template <class Tclass List { private: std::list<T*lst; public: List() { lst = new std::list<T>(); } List(const List<T&rhs) { lst = new std::list<T>(*rhs.lst); } List(int n, const T& value) { lst = new std::list<T>(n, value); }
0
9538
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9975
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9788
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8794
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6623
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5241
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3889
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3481
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2765
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.