473,769 Members | 2,081 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 9794
<ma*********@ho tmail.com> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.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>::it erator 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*********@hot mail.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*********@hot mail.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
3238
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 issue is when the DB value is retrieved it is fine for all integers but for a "Null" value it returns a junk long value and not a pre-defined value, say.
13
3087
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 across "null" string. I know that I will get an "undefined" when I try to retrieve something from the DOM which doesn't exist. I have used null myself to initialize or reset variables. But in which
3
1617
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 value pls check that
8
4335
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 procedure and must save either a null or a real date. However, passing a hardcoded "null" to call this function causes a compilation error : cx.cs(136): Argument '16': cannot convert from '<null>' to 'System.DateTime'
2
1254
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 and I need to distinguish types of passed parameters, which can have "null" values: int Wrapper::Call(String *method, System::Object *args) { // ... // args can be null for(int i = 0, nCount = args->Count; i < nCount; i++)
9
2022
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 is null? I've tried something like this (experimenting with IsDBNull): ========================================================= If dtrData.IsDBNull("Address2") = True Then lblAddress2.Text = "" Else
7
5188
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: I need to define special iterator values. In particular, I want a null iterator. This is different from end() which means the end of this list. I want an iterator value that is known to just not correspond to any element. It must be possible to...
2
2028
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) { return (input == null ? "" : input); }
5
6852
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 huge performance hit when I load the target table with "Not Null" constraints as compared to loading a target table without the constraints.
4
2907
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 is not a null value. I am not using any code behind (C#) to bind the data or manipulate the data. I have read that when there is a null value in the database that there is no record in the "dataset". Can anyone show me how to bind a value in the...
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
10216
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
9997
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
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
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
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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.