Hi,
Does anyone know a good (c++)example of the implementation of a
"robust" list iterator (ie an iterator whereby insert/delete
operations does not have any effect on the list)?
I am looking for an implementation that does not make copies of the list.
thanks in advance,
martin 4 4476
"Martin Smith" <ms****@hotmail .com> wrote in message
news:ct******** **@news2.solcon .nl... Hi,
Does anyone know a good (c++)example of the implementation of a "robust" list iterator (ie an iterator whereby insert/delete operations does not have any effect on the list)?
??? I don't understand ... insert and delete operations will always have "an
effect" on the list, namely, they will add or remove elements. What else
would they do? Perhaps if you give a few more details it would be easier to
understand what you want to do ...
I am looking for an implementation that does not make copies of the list.
Why would an iterator make a copy of the list? I can see why it might store
a reference to the list ...
Dave Moore
O.k sorry for being a bit short.
I am currently implementing the iterator design pattern, from
the book "design patterns" (gamma etc). In the implementation
section for this pattern, it is mentioned that there are many
ways to implement a so called "robust iterator". This iterator
works correct even if delete and insert operators are applied
to the aggregate. Since there are many ways one could implement
this, I was wondering if anyone knows of a good example
for this problem.
kind regards,
martin.
"Dave Moore" <dt*****@email. unc.edu> wrote in message
news:35******** *****@individua l.net... "Martin Smith" <ms****@hotmail .com> wrote in message news:ct******** **@news2.solcon .nl... Hi,
Does anyone know a good (c++)example of the implementation of a "robust" list iterator (ie an iterator whereby insert/delete operations does not have any effect on the list)? ??? I don't understand ... insert and delete operations will always have
"an effect" on the list, namely, they will add or remove elements. What else would they do? Perhaps if you give a few more details it would be easier
to understand what you want to do ...
I am looking for an implementation that does not make copies of the
list. Why would an iterator make a copy of the list? I can see why it might
store a reference to the list ...
Dave Moore
On 2005-01-24 07:44:16 -0500, "Martin Smith" <ms****@hotmail .com> said: O.k sorry for being a bit short. I am currently implementing the iterator design pattern, from the book "design patterns" (gamma etc). In the implementation section for this pattern, it is mentioned that there are many ways to implement a so called "robust iterator". This iterator works correct even if delete and insert operators are applied to the aggregate. Since there are many ways one could implement this, I was wondering if anyone knows of a good example for this problem.
If I understand you correctly, one way that one could implement such an
iterator for a vector-like container is to have the iterator class
contain a pointer to the container, and an index. Then, in the
appropriate functions (operator*, operator[], operator->, etc.) simply
call through to Container::oper ator[], like so:
MyIterator::val ue_type &
MyIterator::ope rator*()
{
return (*m_container)[m_index];
}
.... and have the pointer-arithmatic-like operators (operator++,
operator--, operator +=, etc.) operate on the stored index, like so:
MyIterator &
MyIterator::ope rator++()
{
++m_index;
return *this;
}
Martin Smith wrote: O.k sorry for being a bit short. I am currently implementing the iterator design pattern, from the book "design patterns" (gamma etc). In the implementation section for this pattern, it is mentioned that there are many ways to implement a so called "robust iterator". This iterator works correct even if delete and insert operators are applied to the aggregate. Since there are many ways one could implement this, I was wondering if anyone knows of a good example for this problem.
Even this is a bit on the vague side. The std::list has iterators
which are robust, in the sense that insert never affects such
iterators, and deletion affects only iterators refering to the
deleted item.
Now, it is possible to define an even more robust form of
list iterators, such that deletion of a node will also safely
invalidate the iterators to that node. This requires
registration of the iterators with the node (registration
with the list itself makes deletion O(N) ).
With this registration, it also is possible to invalidate
iterators when the entire list is deleted. This adds yet more
overhead (primarily time, can reuse the per-node data).
Another aspect of robustness is how dereferencing an invalid
iterator fails. The standard makes this UB, but a robust
iterator would probably throw an exception. Other
implementations exist, e.g. an iterator whose value_type
is double may return NaN instead. There may even be situations
in which it makes sense to return iterator::value _type();
Regards,
Michiel Salters This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Vivi Orunitia |
last post by:
Hi all,
I tried looking this up in the sgi docs but it didn't provide any concrete
answer to what I'm looking for. Basically, is there any difference between
using ::iterator for a container vs using ::pointer?
I did a quick experiment and replaced all the ::iterator in my code with
::pointer and it seems to work the same. What I'm think is on the surface
they're the same but perhaps there're some subtle differences beneath the...
|
by: Dom Gilligan |
last post by:
What exactly can you assign to a set iterator, by assignment or
initialisation? (is this covered in Josuttis? I can't find it).
Some (limited) digging around in the Gnu code shows no operator=, but
2 ctors for iterators, and 3 for const_iterators. For const_iterator,
there's a no-parameter ctor, and a ctor which takes another
const_iterator. The 3rd one is more interesting - it takes a pointer
to a node in the R/B tree, and the iterator...
|
by: Dan Tsafrir |
last post by:
is the following code standard? (cleanly compiles under g++-4.0.2):
struct Asc { bool operator()(int a, int b) {return a < b;} };
struct Des { bool operator()(int a, int b) {return b > a;} };
int main() {
int arr = {1, 2, 3};
set<int,Asc> asc(arr, arr+3);
set<int,Des>::iterator beg = asc.begin(); //
set<int,Des>::iterator end = asc.end(); //
copy(beg, end, ostream_iterator<int>(cout,...
|
by: flopbucket |
last post by:
Hi,
For the learning experience, I am building a replacement for std::map.
I built a templated red-black tree, etc., and all the basic stuff is
working well. I implemented basic iterators and operator++, etc., and
begin(), end(), and it works as a drop in for std::map if you stick to
the methods I have implemented.
Anyway, I decided to go and add const_iterator support, thinking that
it would be simple, but I ran into a confusing...
|
by: mailforpr |
last post by:
Hi.
Let me introduce an iterator to you, the so-called "Abstract Iterator"
I developed the other day.
I actually have no idea if there's another "Abstract Iterator" out
there, as I have never looked for one on the net (I did browse the
boost library though). It doesn't matter right now, anyway.
To put it simply, Abstract Iterator is mainly a wrapper class. It helps
| |
by: T.A. |
last post by:
I understand why it is not safe to inherit from STL containers, but I have
found (in SGI STL documentation) that for example bidirectional_iterator
class can be used to create your own iterator classes by inheriting from
it, ie.
class my_bidirectional_iterator : public bidirectional_iterator<double>
{
...
};
|
by: jayesah |
last post by:
Hi All,
List and its iterator work as following way :
list<intmylist;
list<int>::iterator itr;
itr = mylist.begin();
cout << (*itr);
But I want something like this:
|
by: =?gb2312?B?wNbA1rTzzOzKpg==?= |
last post by:
windows xp, visual studio 2005
----------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<int, int>::iterator it = 0;
if( it != 0 ) //break point,
|
by: Juha Nieminen |
last post by:
I'm actually not sure about this one: Does the standard guarantee
that if there's at least one element in the data container, then
"--container.end()" will work and give an iterator to the last
element in the container?
Or is there a cleaner way of getting an iterator to the last
element?
|
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...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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();...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |