473,769 Members | 7,650 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to find out a if an iterator is pointing to the very last (end) node?

How do I do that?

The thing is, the only information I have about the iterator is the
iterator itself. No container it is belonging to or anything. Like

template<Iterat orvoid totally_isolate d(Iterator& it)
{
//how do I find out if it points to the end node?
}

Iterators like std::vector::it erator, std::list::iter ator and so on are
usually passed to the totally_isolate d() function. User defined
iterators may indeed be passed to the function as well.

Is there any safe and portable solution? This is my goal: I want to
access the referenced value via the *-operator. I can't do this if if
the iterator is pointing to the very last node (if I did, an exception
would be thrown or an assertion would fail).

Instead of retrieving the referenced value, I want the function to do
something 'special' (like printing a message to the screen) whenever
the iterator is pointing to the end node.

Is there any trick?

Oct 5 '06 #1
16 2583
ma*******@googl email.com wrote:

What is the sound of one hand clapping?

A pair of iterators designates a sequence of values. A single iterator,
without context, tells you nothing.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Oct 5 '06 #2
A pair of iterators designates a sequence of values. A single iterator,
without context, tells you nothing.
Pete, even if I had a pair of iterators, it would be no use. Would be
still the same problem.

Oct 5 '06 #3
ma*******@googl email.com wrote:
>A pair of iterators designates a sequence of values. A single iterator,
without context, tells you nothing.

Pete, even if I had a pair of iterators, it would be no use. Would be
still the same problem.
If the two iterators in the pair are equal, the sequence is empty, and
the first iterator does not point to a valid element. If the two are not
equal, the first iterator points to a valid element.

It sounds like the actual problem is that the code is using iterators in
non-idiomatic ways. Don't do that.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Oct 5 '06 #4
In article <11************ **********@c28g 2000cwb.googleg roups.com>,
ma*******@googl email.com wrote:
How do I do that?

The thing is, the only information I have about the iterator is the
iterator itself. No container it is belonging to or anything. Like

template<Iterat orvoid totally_isolate d(Iterator& it)
{
//how do I find out if it points to the end node?
}

Iterators like std::vector::it erator, std::list::iter ator and so on are
usually passed to the totally_isolate d() function. User defined
iterators may indeed be passed to the function as well.

Is there any safe and portable solution? This is my goal: I want to
access the referenced value via the *-operator. I can't do this if if
the iterator is pointing to the very last node (if I did, an exception
would be thrown or an assertion would fail).

Instead of retrieving the referenced value, I want the function to do
something 'special' (like printing a message to the screen) whenever
the iterator is pointing to the end node.

Is there any trick?
SOP is to simply declare such an error as undefined. IE: 'it' must be a
valid iterator in a range.

If you are using an implementation that has checked iterators, they
would be able to notify you of the problem, but that's not part of the
language.

The only other thing I can think to do off hand is change your function
to take a container and iterator instead of just an iterator.

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer*have anyone to discuss your doubts with,
nor any ability to discuss them.
Oct 5 '06 #5

<ma*******@goog lemail.comwrote in message
news:11******** **************@ c28g2000cwb.goo glegroups.com.. .
How do I do that?

The thing is, the only information I have about the iterator is the
iterator itself. No container it is belonging to or anything. Like

template<Iterat orvoid totally_isolate d(Iterator& it)
{
//how do I find out if it points to the end node?
}
What's the "end node"? When an iterator is equal to mycontainer.end (), it's
pointing one PAST the last valid item in mycontainer. And without
mycontainer, there's not way to test for equality with mycontainer.end (),
obviously.
>
Iterators like std::vector::it erator, std::list::iter ator and so on are
usually passed to the totally_isolate d() function. User defined
iterators may indeed be passed to the function as well.
Iterators are not generic. How would you define a function that took just
any old iterator?
Is there any safe and portable solution? This is my goal: I want to
access the referenced value via the *-operator. I can't do this if if
the iterator is pointing to the very last node (if I did, an exception
would be thrown or an assertion would fail).
You can't access what an iterator points to without knowing the iterator
type in the first place. What code do you have that lets you do that?
>
Instead of retrieving the referenced value, I want the function to do
something 'special' (like printing a message to the screen) whenever
the iterator is pointing to the end node.

Is there any trick?
Yes. Don't do that. :-)

What problem are you trying to solve, that you need to pass a "generic"
iterator to, and get what it's pointing to? Probably, you shouldn't be
calling this function in the first place, if you're past the end of the
container. Are you in a loop? What's the code look like that could end up
trying to use an iterator which is equal to .end()?

We might be able to help you find a solution that doesn't cause this problem
for you, if we knew what the real problem was you were trying to solve.
Some code might be nice. :-)

-Howard

Oct 5 '06 #6
"Howard" <al*****@hotmai l.comwrote:
<ma*******@goog lemail.comwrote :
Iterators like std::vector::it erator, std::list::iter ator and so on are
usually passed to the totally_isolate d() function. User defined
iterators may indeed be passed to the function as well.

Iterators are not generic. How would you define a function that took just
any old iterator?
Is there any safe and portable solution? This is my goal: I want to
access the referenced value via the *-operator. I can't do this if if
the iterator is pointing to the very last node (if I did, an exception
would be thrown or an assertion would fail).

You can't access what an iterator points to without knowing the iterator
type in the first place. What code do you have that lets you do that?
What? Excepting input and output iterators, if you write code that works
with a forward iterator, then it might as well be generic because all
other iterators will work.

To access what an iterator points to, you simply dereference it.

I'm afraid your point is lost on me... :-/

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer*have anyone to discuss your doubts with,
nor any ability to discuss them.
Oct 5 '06 #7

"Daniel T." <da******@earth link.netwrote in message
news:da******** *************** *****@news.west .earthlink.net. ..
"Howard" <al*****@hotmai l.comwrote:
><ma*******@goo glemail.comwrot e:
Iterators like std::vector::it erator, std::list::iter ator and so on are
usually passed to the totally_isolate d() function. User defined
iterators may indeed be passed to the function as well.

Iterators are not generic. How would you define a function that took
just
any old iterator?
Is there any safe and portable solution? This is my goal: I want to
access the referenced value via the *-operator. I can't do this if if
the iterator is pointing to the very last node (if I did, an exception
would be thrown or an assertion would fail).

You can't access what an iterator points to without knowing the iterator
type in the first place. What code do you have that lets you do that?

What? Excepting input and output iterators, if you write code that works
with a forward iterator, then it might as well be generic because all
other iterators will work.

To access what an iterator points to, you simply dereference it.

I'm afraid your point is lost on me... :-/
The OP seemed to imply that any old iterator could be used somehow, which to
me meant any old iterator which points to any old object. I may have
misinterpreted the OP's meaning there. I was wondering how you could _use_
an iterator which referenced an unkown type. Since you're writing code in
that function which actually _does_ something with that object, somebody has
to know what type of object it is, right? (Even template variables
eventually execute type-specific code.)

And taking that thought further, since you know what type of object you're
using (or at least you have a template variable of a generic type), then
instead of passing an iterator to the function, the OP should pass the
object itself, and put any controlling code (such as checking against
..end()) in the calling code, not in that function.

In any case, removing the iterator from the function entirely makes the
problem of checking against .end() move to where it should be: in the hands
of someone who is using the actual container! (It also allows one to use
any kind of iterator, whether it's forward, reverse, random-access or
whatever, because the iterator isn't passed to the function in the first
place.)

-Howard
Oct 5 '06 #8
Thanks for your help, folks. I appreciate it.

Howard wrote:
What's the "end node"? When an iterator is equal to mycontainer.end (), it's
pointing one PAST the last valid item in mycontainer. And without
mycontainer, there's not way to test for equality with mycontainer.end (),
obviously.
Yeah, with "end node" I mean the one that equals the node of
mycontainer.end (). Unfortunately (or fortunately) it's not legal to
dereference the mycontainer.end () iterator.
Iterators are not generic. How would you define a function that took just
any old iterator?
Well, as long as I presume that any iterator has these operators:

operator*
operator->
operator++
operator--

everything should be fine. I don't make use of operatoror operator<,
as not every iterator type supports them.

But in this case (the totally_isolate d() function case), I only make
use of the *-operator. I don't even need to know the underlying type
the iterator operates on (see below.)
You can't access what an iterator points to without knowing the iterator
type in the first place. What code do you have that lets you do that?
In fact, the totally_isolate d() function is a member of a class to
which the iterator type is actually known. Here is the class I am
talking about:
http://groups.google.com/group/comp....browse_thread/
thread/d908a39b187d60e 6/42558e69440c1da 1?lnk=gst&rnum= 1#42558e69440c1 da1

I'll try to explain the current problem, so you don't have to waste
your time examining the crappy code.

These Abstract Iterator classes wrap iterators. Once an Abstract
Iterator has "swallowed" an iterator, no one but itself knows what the
type of the wrapped iterator is (e.g. vector<...>::it erator,
list<...>::iter ator, ...).

When it comes to comparing two Abstract Iterators, I can only pass an
abstract iterator in the shape of a base pointer to another abstract
iterator, which then tests (via dynamic_cast) if the passed base
pointer is pointing to an abstract iterator with the same underlying
iterator type. Then they can be compared. But I don't like the use of
dynamic cast, because once the wrong abstract iterator is passed, an
exception is thrown.

So I have the abstract iterators return addresses of the objects their
wrapped iterators are pointing to so I can compare these addresses. The
functions that return the generic (void*) addresses are called
get_value_addre ss(). It worked, until one of the wrapped iterators
happened to point to an end node. Here's the implementation of that
function that returns the generic address:

void const* get_value_addre ss() const
{
return &*it;
}

That's why I need to dereference the iterator (actually, 'it' is a
member of the class rather than a parameter as shown in the
totally_isolate d() function. Still, the whole Abstract Iterator concept
is ignorant about the iterator that is hold as a member. But it would
lose a lot of its elegance if I forced the user of the Abstract
Iterator to pass a reference to the container to an Abstract Iterator
object as well.

Anyway: I need to return the address of the object the 'it' iterator is
pointing to to compare two Abstract Iterators. (I am aware of other
ways to compare two Abstract Iterators, but they make use of
dynamic_cast, which is not so elegant. But I have to, if there's no way
to determine if 'it' is pointing to mycontainer.end ().)

by and large, comparison is done this way:
// Instead of comparing two iterators the usual way like:
....
std::vector<int >::iterator i1 = vec.begin(), i2 = vec.begin();
i1 == i2;

// I have the iterators compared like this:

(&(*i1)) == (&(*i2))

But if either of these iterators happens to point to the end node, the
prorgam crashes.

I wished the C++ language stored "real" type information rather than
pseudo type_infos, which only contain lousy strings. So I could write

typeinfo iterator_type = typeid(std::vec tor<int>::itera tor);
std::vector<int >::iterator& iter =
static_cast<ite rator_type&>(di sguised);

There would be no dynamic_cast whatsoever.

Oct 6 '06 #9
ma*******@googl email.com wrote:
How do I do that?

The thing is, the only information I have about the iterator is the
iterator itself. No container it is belonging to or anything. Like

template<Iterat orvoid totally_isolate d(Iterator& it)
{
//how do I find out if it points to the end node?
}
Ok, let's take the most basic iterator of all: int*.
It's the natural iterator for an int[10]. So, how do you know if an
int* points
to one after the int[10]?

HTH,
Michiel Salters

Oct 6 '06 #10

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

Similar topics

4
1892
by: matthurne | last post by:
I am working through exercise 8-2 in Accelerated C++...I am implementing the function equal(b, e, b2) where b is an iterator for the first element in a container, e is an iterator pointing to one past the last element in that same container, and b2 is an iterator for the first element in the second container. Here's what I have: template <class T> bool equal(T begin, T end, T begin2) { while (begin != end) { if (*begin != *begin2) {
29
3975
by: Hagen | last post by:
Hello, in a recent thread "speed of vector vs array" I read about the problem of the slow acces by addressing vector elements by indexing, unfortunately I see no workaround in my case. My case: class A {
0
1945
by: nick | last post by:
Hi, I need to manage a "layered" collection of objects, where each layer grows independently, e.g, o--+--+--+--+--+ 1st layer | o 2nd layer (empty) | o--+--+--+ 3rd layer |
5
2625
by: Mark Stijnman | last post by:
I have a question about forward iterators and what one should do or not do with them. I'm planning on writing a container that, when all boils down to it, stores a sequence of strings. I want threads to be able to read this sequence from start to end, and when they reach the end, wait until new data is added to the end. If so, it should pick up reading where it left off. The question is, is it valid and moral to do something like this from...
5
2578
by: Draw | last post by:
Hi All, Just a thought, about the find() algorithm in the C++ STL. I read that the find algorithm can take a range of iterators. If it does not find the element it is looking for in that range it returns the iterator to the last element in the range, not to the last element in the container, to the last element in the range. That being said, how can we tell if find() has been successful in finding the element we need? Its easy when we...
18
2564
by: silversurfer | last post by:
Ok, this should be fairly easy for most of you (at least I hope so), but not for me: Let us say we have got the following elements: std::vector<Entry> models; //Entry is a struct std::vector<Entry>::iterator modelIterator; In a method, I am currently writing, I need to get a pointer to an entry in the vector.. I thought of the following:
15
25488
by: Boltar | last post by:
Hi I'm going through an STL list container using a reverse iterator but it seems the erase() method only accepts ordinary iterators. Is there a similar method that will accept reverse iterators or alternatively is there a way to convert a reverse iterator to a normal one? Thanks for any help B2003
4
3899
by: TBass | last post by:
Hi, I've got a class that uses a std::list to store tags that need to be written out to a device. I've had no problem with the lists in terms of iterating and reading values. My problem is when I need to pop that value off the list when I'm done with it. In this case, I think the iterator is getting "lost" because I pop off the value it is pointing to at the end of this function. But how do I pop values off one at a time without the...
5
1659
by: remlostime | last post by:
struct nodeType { int v, index; }; list<nodeTypenode; for(list<nodeType>::iter = node].begin(); iter != node].end(); iter++) What's wrong with it? how can i fix it?
0
9589
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
9423
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,...
0
10214
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9996
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,...
0
9865
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
8872
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
5304
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
3963
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
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.