473,499 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

stl iterators "null" value

Dear Sirs, Dear Newsgroup,

Imagine I have some function that only gives me an iterator to a
vector, but not the vector itself. Unfortunately this vector can be
empty and the iterator can point to 'non valid'.

Yes I know this is rather silly, but I did not write the code...

Normally I know you would check for "iterator != vector.end()", but
since I do not seem to have the vector without changing a lot of old
code, I wonder what to do. Comparing the iterator to 0 does not seem to
be acceptable for the compiler.

My thanks in advance, Marc

Dec 30 '05 #1
6 9770
<ma*********@hotmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com
Dear Sirs, Dear Newsgroup,

Imagine I have some function that only gives me an iterator to a
vector, but not the vector itself. Unfortunately this vector can be
empty and the iterator can point to 'non valid'.

Yes I know this is rather silly, but I did not write the code...

Normally I know you would check for "iterator != vector.end()", but
since I do not seem to have the vector without changing a lot of old
code, I wonder what to do. Comparing the iterator to 0 does not seem
to be acceptable for the compiler.

My thanks in advance, Marc


I think this is a dreadful design and personally I would be in favour of
"changing a lot of old code".

If that is not possible, then I think you will have to accept a non-portable
solution. Looking at the implementation on VC++, an iterator that points to
an empty vector has an internal pointer that equals zero. You can get this
pointer using operator->(). Checking to see if this is zero gives the
desired result. For example:

#include <vector>
#include <iostream>

using namespace std;
int main()
{
vector<int> vec;
vector<int>::iterator it = vec.begin();

if(it.operator->() == 0)
cout << "empty\n";
else
cout << "not empty\n";

return 0;
}
--
John Carson
Dec 30 '05 #2
John:
I think this is a dreadful design and personally I would be in favour of
"changing a lot of old code".


I have to agree with you here, and will find a way to get the vector.
Using that internal pointer is rather uncommon code, and a future
modifier of this code will probably not understand it.

Making the whole thing clearer is the best option, although an quick
solution would have been nice.

Dec 30 '05 #3
ma*********@hotmail.com wrote:
Dear Sirs, Dear Newsgroup,

Imagine I have some function that only gives me an iterator to a
vector, but not the vector itself. Unfortunately this vector can be
empty and the iterator can point to 'non valid'.

Yes I know this is rather silly, but I did not write the code...

Normally I know you would check for "iterator != vector.end()", but
since I do not seem to have the vector without changing a lot of old
code, I wonder what to do. Comparing the iterator to 0 does not seem to
be acceptable for the compiler.


The function could not be returning an "iterator" from the client's
point of view, since without knowing how many items are in the
container, the value returned could not be used to iterate the contents
of a container safely.

The function must be returning some kind of reference to an item stored
in a container - the reference could be a pointer to the item itself or
it could even be a pointer to an iterator for the item (which seems to
be the case here).

Since it is possible for the function to return no item, the function
result must be in the form of a pointer (either to the item itself or
to its iterator). The latter approach just adds an extra, unnecessary
layer of indirection. But given the constraints described, the function
has to return a pointer to something (from which the item can be
obtained) in order to allow the function to return NULL (for those
occasions when the function has no item to return).

Greg

Dec 30 '05 #4
Greg:
The function could not be returning an "iterator" from the client's
point of view, since without knowing how many items are in the
container, the value returned could not be used to iterate the contents
of a container safely.


I already solved the problem, but to clarify. The iterator was written
such that it always returned exactly one item. Now other code has
changed and the iterator can point to nothing. But now I got another
function that gives me the vector, so I neatly compare to ...->end();

Dec 30 '05 #5

ma*********@hotmail.com wrote:
Dear Sirs, Dear Newsgroup,

Imagine I have some function that only gives me an iterator to a
vector, but not the vector itself. Unfortunately this vector can be
empty and the iterator can point to 'non valid'.
No, it can't.
However, I fail to see the probelm. So what if a vector is empty?
It still has a .begin() and an .end(). They just happen to be the same.
Obviously, since you can't read from .end() this implies you can't
read from .begin() either. Makes perfect sense for an empty vector.
Returning such an iterator is also perfecly legal.
Normally I know you would check for "iterator != vector.end()", but
since I do not seem to have the vector without changing a lot of old
code, I wonder what to do. Comparing the iterator to 0 does not seem to
be acceptable for the compiler.


What use is a vector iterator anyway without .end() ? You can't iterate
with it, and you can't access its value (because it might just be
..end())
So that excludes operator++ and operator*, the two members that are
at the core of the iterator concept.

HTH,
Michiel Salters

Jan 2 '06 #6
> I fail to see the

Dear Michiel, I solved this problem days ago. Don't spend your time on
it.

Jan 2 '06 #7

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

Similar topics

1
3228
by: Denzil | last post by:
Hi, I am retrieving my DB values and setting them in the MyRS::DoFieldExchange(CFieldExchange* pFX) function. One of these records is a Long datatype and could be "Null" in the DB table. The...
13
3061
by: Don Vaillancourt | last post by:
What's going on with Javascript. At the beginning there was the "undefined" value which represented an object which really didn't exist then came the null keyword. But yesterday I stumbled...
3
1600
by: Karunakararao | last post by:
Hi all Presently i am sending data to database filed like this "EquipmentFilterDevPrimaryId = 0" i need Instead of "0" (NULL)i need store the data null value how can i pass this null...
8
4316
by: craigkenisston | last post by:
I have a generic function that receives a couple of datetime values to work with. They can or cannot have a value, therefore I wanted to use null. This function will call a database stored...
2
1240
by: Stanislav Simicek | last post by:
Hello, I'd like to know, whether it is possible to have type information associated with "null". I am implementing a wrapper (MC++) for our legacy method that accepts variable number of arguments...
9
2001
by: D. Shane Fowlkes | last post by:
I'm using SQL Server 2000 and on my page, I'm simply creating a SQLDataReader and filling in Labels with the retrieved (single) record. However, how can I prevent from getting errors when a field...
7
5164
by: andreas | last post by:
Hello, I have a problem with iterators in a fairly complex polygonal mesh data structure which is implemented using lists of geometric entities. However, the problem in itself is fairly simple:...
2
2010
by: Jeff | last post by:
Hey I've bought the book "ASP.NET 2.0 website programming, Problem, Design, Solution" and some of its code examples is this code: protected static string ConvertNullToEmptyString(string input)...
5
6822
by: yeoj13 | last post by:
Hello, I have a db2load script I'm using to populate a large table. Ideally, my target table is required to have "Not Null" constraints on a number of different columns. I've noticed a ...
4
2875
by: justice750 | last post by:
Hi All, I am using a FormView control. The allows me to update records in the database. However, when a database field is null I can not update the field on the form. It works fine when the field...
0
7134
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
7014
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
7180
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,...
1
6905
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...
0
5485
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,...
1
4921
isladogs
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...
0
4609
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...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
311
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...

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.