473,395 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

null or nil or initialised std::list iterator?

I want to know how to set an std::list iterator variable to make it
null or nil. If this is not possible what is the value of an
uninitialised std::list iterator and is it ok to assign this value to a
std::list iterator variable (or is it better to use a seperate bool
variable as a "null flag" ?)

e.g.
struct mystru
{
std::string nm;
int qty;
}

std::list<mystru>::iterator i;

// what is the value of i ?
....
i = somelist.begin();
// do things with i
....
i = NULL_VALUE?

Aug 30 '05 #1
15 19623

sa************@hotmail.com wrote:
I want to know how to set an std::list iterator variable to make it
null or nil. If this is not possible what is the value of an
uninitialised std::list iterator and is it ok to assign this value to a
std::list iterator variable (or is it better to use a seperate bool
variable as a "null flag" ?)

e.g.
struct mystru
{
std::string nm;
int qty;
}

std::list<mystru>::iterator i;

// what is the value of i ?
unspecified. you CAN'T use i without giving it a value
...
i = somelist.begin();
// do things with i
...
i = NULL_VALUE?


i = somelist.end(); // the end iterator of a container is one-past the
end of the container

Aug 30 '05 #2

Alipha wrote:

unspecified. you CAN'T use i without giving it a value


I think that is the point, I want to know when it does not
have a value or does that count as using it too?

what about iterator(NULL) ?

Aug 30 '05 #3
sa************@hotmail.com wrote:
unspecified. you CAN'T use i without giving it a value


I think that is the point, I want to know when it does
not have a value or does that count as using it too?

what about iterator(NULL) ?


Use a pointer to iterator, set it to 0 to signal absence.
iterators don't have a Null State.

Marc

Aug 30 '05 #4
<sa************@hotmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
I want to know how to set an std::list iterator variable to make it
null or nil.
You can't.
If this is not possible what is the value of an
uninitialised std::list iterator and is it ok to assign this value to a
std::list iterator variable (or is it better to use a seperate bool
variable as a "null flag" ?)


If you try to copy an uninitialized std::list iterator, the effect is
undefined.

Here's one way to do what I think you want:

std::list<T> marker;
marker.push_back(T());

Now you have a list named marker that has a single element.

The value of marker.begin() is therefore an iterator that refers to that
element. No other iterator will ever be equal to that value.

So you can write

std::list<T>::iterator nulliterator = marker.begin();

and then use the value of nulliterator as if it were a null iterator.

You will have to repeat this process for each distinct type T you intend to
use. Of course you can use templates to simplify this repetition.
Aug 30 '05 #5
sa************@hotmail.com wrote:
Alipha wrote:
unspecified. you CAN'T use i without giving it a value

I think that is the point, I want to know when it does not
have a value or does that count as using it too?

what about iterator(NULL) ?


Nope. Learn the iterator idioms: a pair of iterators designates a range
of values. Lone iterators aren't very useful.

What is the actual problem that you're trying to solve?

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Aug 30 '05 #6
> Use a pointer to iterator, set it to 0 to signal absence.
iterators don't have a Null State.


Thanks for your help, although not the answer I wanted to hear.
That is so annoying, I am going to rant now. Lots of algorithms that
use lists, graphs, trees etc are implemented with "nil" or "null"
pointer states thus avoiding source code clutter. Who designed these
delicate STL iterators anyway? In many cases I guess it would be
better to use a traditional pointer based implementation instead of the
STL.

Aug 30 '05 #7
I want to store a reference to a std::list node.

Aug 30 '05 #8
... that could be null.

Aug 30 '05 #9
<sa************@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I want to store a reference to a std::list node.
.. that could be null.


Then you may use a pointer to the list node - not an iterator.

Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Aug 30 '05 #10

Ivan Vecerina wrote:
Then you may use a pointer to the list node - not an iterator.

But how do I do that, isnt the node implementation hidden from me.

Aug 30 '05 #11
<sa************@hotmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
Ivan Vecerina wrote:
Then you may use a pointer to the list node - not an iterator.

But how do I do that, isnt the node implementation hidden from me.


Any iterator can be converted to a pointer by taking the address
of the dereferenced iterator. Given (based on your OP):
std::list<mystru>::iterator i = ...;
the address of the item pointed to by 'i' can be obtained with:
mystru* p = &*i; // or &(*i) if you prefer

Of course, you cannot use p to iterate through the container,
and you cannot (easily) convert it back to an iterator.
But this solution is the easiest if you only need to later
access that one item.

Cheers, Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Aug 30 '05 #12
Looking at the algorithm I would want to convert it back. Now I
realise I should be using a graph instead of a double linked list
because I need a custom pointer as well as "next".

Aug 30 '05 #13
sa************@hotmail.com wrote:
I want to store a reference to a std::list node.


That's not the actual problem, it's your attempt at a solution. Pop up a
level and talk about what your code is supposed to do, rather than how
it tries to do it. Why do you need to store a possibly null reference to
a list node?

Ultimately, if you're fighting with the design of the library that
you're using, you need to either change the approach that you're taking
or change the library.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Aug 30 '05 #14
<sa************@hotmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Looking at the algorithm I would want to convert it back. Now I
realise I should be using a graph instead of a double linked list
because I need a custom pointer as well as "next".

Note: as a courtesy to others, please quote relevant parts of
the post you are replying -- to provide context.

If you only have one collection of items, the end() iterator
is the invalid value you are looking for. Additionally,
Andrew Koenig has provided a solution if you really need an
'invalid' iterator independent of a container instance.

But as it appears you are not sure what container you should
be using, you should consider describing the algorithm that
you are trying to implement. This will allow others to provide
high-level advice instead of answers to naively focused
questions.

Cheers,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Aug 30 '05 #15
Using "nil" to denote "nothing left" is an implementation detail. C++
has a different implementation, usually .end().

STL iterators are hardly delicate. I often find myself wishing other
languages supported C++-style iterators.

As an example of how to turn a nil-terminated list algorithm into a
..end()-terminated list algorithm, I submit a (reimplementation) of the
classic merge algorithm, compared to an SML/NJ version.

===== SML/NJ merge algorithm =====
fun merge(x::xs, y::ys) = if (x < y) then x::merge(xs, y::ys) else
y::merge(x::xs, ys) |
merge(nil, y::ys) = y::ys |
merge(x::xs, nil) = x::xs |
merge(nil, nil) = nil;
===== end SML/NJ =====
===== Full C++ for a merge example =====
#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>
#include <list>

using std::cout;
using std::endl;
using std::copy;
using std::ostream_iterator;
using std::vector;
using std::list;
using std::back_inserter;

template <typename T1, typename T2, typename T3>
void my_merge(T1 begin1, T1 end1, T2 begin2, T2 end2, T3 copy_to)
{
while (begin1 != end1 and begin2 != end2) {
if (*begin1 < *begin2)
*copy_to++ = *begin1++;
else
*copy_to++ = *begin2++;
}
if (begin1 != end1)
copy(begin1, end1, copy_to);
if (begin2 != end2)
copy(begin2, end2, copy_to);
}

int main(void)
{
const int FOO[] = { 0, 2, 4, 6 };
const int BAR[] = { 1, 3, 5, 7 };
vector<int> foo(FOO, FOO + 4);
list<int> bar(BAR, BAR + 4);
vector<int> result;
my_merge(foo.begin(), foo.end(), bar.begin(), bar.end(),
back_inserter(result));
copy(result.begin(), result.end(), ostream_iterator<int>(cout, "\t"));
cout << endl;
return 0;
}
===== end C++ =====
.... As you can tell, it's the same algorithm in both instances. The
differences are purely in implementation, not in conception. That's
good news for you, because implementation differences are almost always
surmountable. Conceptual differences are much, much harder.

Aug 30 '05 #16

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

Similar topics

8
by: ma740988 | last post by:
Consider: # include <iostream> using std::cout; using std::cin; using std::endl; # include <list> using std::list;
6
by: PengYu.UT | last post by:
Hi, Suppose I have a list which contains pointers. I want the pointer got by dereferencing the iterator be a pointer pointing to a const object. But std::list<const T*>::const_iterator doens't...
5
by: gerg | last post by:
I'm having to deal with some legacy code which I think may be causeing some problems. Can STL pro's please add comments about the correctness of the following code: class A { public: /// ......
15
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:
2
by: raven.mp4 | last post by:
Hi, I have a little question about std::list iterators. Take a look at this piece of code: list<Type*>::iterator it = myList.begin(); Type *p; for(it; it != myList.end(); ++it) {
19
by: Juha Nieminen | last post by:
If I'm not completely mistaken, the only reason why std::list::size() may be (and usually is) a linear-time operation is because they want std::list::splice() to be a constant-time operation, and...
5
by: Christopher | last post by:
The situation is that a std::list<std::set<std::string is being iterated through. Upon certain criteria some sets become empty. I need to remove the empty sets from the list. Is it safe to...
7
by: TBass | last post by:
So I have a class: class Client { unsigned int ClientID; .... }; class MyListenSocket
12
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();
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...

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.