473,804 Members | 2,455 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

iterator error

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,
this is an run time error?
cout<<"ok!";
}
----------------------------------------------------------------------------------------------------------------------------------
why it can be assign "0", but can't compare with "0"?

Apr 6 '07 #1
22 1579
ÀÖÀÖ´óÌìʦ wrote:
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,
this is an run time error?
cout<<"ok!";
}
----------------------------------------------------------------------------------------------------------------------------------
why it can be assign "0", but can't compare with "0"?
The iterator type in 'map' is implementation-defined. What
it means to initialise it with (int)0 is implementation-defined.
Why operator != (int)0 doesn't work is (you guessed it!)
implementation-defined. You need to either look at the code
in the debugger to see what's going on or ask in the newsgroup
dedicated to your implementation (microsoft.publ ic.vc.* family
of newsgroups come to mind).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 6 '07 #2
ÀÖÀÖ´óÌìʦ wrote:
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,
this is an run time error?
cout<<"ok!";
}
----------------------------------------------------------------------------------------------------------------------------------
why it can be assign "0", but can't compare with "0"?
That all depends on what the type of a std::map<int, int>::iterator is
and whether it cam be initialised by or compared to an int. If it lacks
a public constructor, the assignment is illegal and if it lacks an
operator==(int) , the comparison is illegal.

--
Ian Collins.
Apr 6 '07 #3
Ian Collins wrote:
ÀÖÀÖ´óÌìʦ wrote:
>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,
this is an run time error?
cout<<"ok!";
}
----------------------------------------------------------------------------------------------------------------------------------
why it can be assign "0", but can't compare with "0"?
That all depends on what the type of a std::map<int, int>::iterator is
and whether it cam be initialised by or compared to an int. If it
lacks a public constructor, the assignment is illegal and if it lacks
an operator==(int) , the comparison is illegal.
It's actually operator!=(int) we're discussing here... Unless you think
they are related somehow...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 6 '07 #4
Victor Bazarov wrote:
Ian Collins wrote:
>>
That all depends on what the type of a std::map<int, int>::iterator is
and whether it cam be initialised by or compared to an int. If it
lacks a public constructor, the assignment is illegal and if it lacks
an operator==(int) , the comparison is illegal.


It's actually operator!=(int) we're discussing here... Unless you think
they are related somehow...
Typo.

--
Ian Collins.
Apr 6 '07 #5
"ÀÖÀÖ´óÌìʦ " <ch*********@gm ail.comwrote in message
news:11******** *************@b 75g2000hsg.goog legroups.com...
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,
this is an run time error?
cout<<"ok!";
}
----------------------------------------------------------------------------------------------------------------------------------
why it can be assign "0", but can't compare with "0"?
Read the other's comments. You are trying to compare an iterator and an
integer. On some (most?) implementations it = 0 would probably be invalid
also. The correct usage is to initialize them with .begin() or .rbegin() or
some iterator value. For example:

int main()
{
std::map<int, intMyMap;
std::map<int, int>::iterator it = MyMap.begin();
// code
}

persontally, I normally use iterators mostly in for loops to iterate through
a container (vector or map) or to get a pointer to a newly inserted member.
Such as:

typedef std::map<int, intIntMap;
IntMap MyMap;
// fill MyMap
for ( IntMap::iterato r it = MyMap.begin(); it != MyMap.end(); ++it )
std::cout << it->first << "-" << it->second << "\n";
Apr 6 '07 #6
ÀÖÀÖ´óÌìʦ wrote:
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,
this is an run time error?
cout<<"ok!";
}
----------------------------------------------------------------------------------------------------------------------------------
why it can be assign "0", but can't compare with "0"?
Repeat after me. Iterators are NOT POINTERS. Again. Iterators are NOT
POINTERS.

There is no such thing as a "0" or NULL iterator. Any valid iterator
must refer to a container.

Apr 6 '07 #7
On Apr 6, 3:02 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
ÀÖÀÖ´óÌìʦ wrote:
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,
this is an run time error?
cout<<"ok!";
}
----------------------------------------------------------------------------------------------------------------------------------
why it can be assign "0", but can't compare with "0"?
The iterator type in 'map' is implementation-defined. What
it means to initialise it with (int)0 is implementation-defined.
Why operator != (int)0 doesn't work is (you guessed it!)
implementation-defined.
It's not even implementation-defined, it's undefined.

Most good implementations will not compile either of the above
lines.
You need to either look at the code
in the debugger to see what's going on or ask in the newsgroup
dedicated to your implementation (microsoft.publ ic.vc.* family
of newsgroups come to mind).
Actually, he needs to change his code. It might help if he
explained what he is trying to accomplish.

--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 6 '07 #8
James Kanze wrote:
On Apr 6, 3:02 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
>ÀÖÀÖ´óÌìʦ wrote:
>>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,
this is an run time error?
cout<<"ok!" ;
}
----------------------------------------------------------------------------------------------------------------------------------
why it can be assign "0", but can't compare with "0"?
>The iterator type in 'map' is implementation-defined. What
it means to initialise it with (int)0 is implementation-defined.
Why operator != (int)0 doesn't work is (you guessed it!)
implementati on-defined.

It's not even implementation-defined, it's undefined.
Strictly speaking without an implementation it wouldn't even exist and
therefore would not compile
>
Most good implementations will not compile either of the above
lines.
>You need to either look at the code
in the debugger to see what's going on or ask in the newsgroup
dedicated to your implementation (microsoft.publ ic.vc.* family
of newsgroups come to mind).

Actually, he needs to change his code. It might help if he
explained what he is trying to accomplish.

--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
I agree the code is somewhat dodgy and probably not portable

JB
Apr 6 '07 #9
red floyd wrote:
ÀÖÀÖ´óÌìʦ wrote:
>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,
this is an run time error?
cout<<"ok!";
}
----------------------------------------------------------------------------------------------------------------------------------
why it can be assign "0", but can't compare with "0"?
Repeat after me. Iterators are NOT POINTERS. Again. Iterators are NOT
POINTERS.

There is no such thing as a "0" or NULL iterator. Any valid iterator
must refer to a container.
So what does the default constructor for an iterator refer to? Just
curious as a valid container may not yet exist

JB
Apr 6 '07 #10

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

Similar topics

38
3697
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages mentioned: I characterized one way of looking at languages in this way: a lot of them are either the agglutination of features or they're a crystallization of style. Languages such as APL, Lisp, and Smalltalk are what you might call style...
3
2223
by: John Smith | last post by:
Hey I have some code which I've been using on Microsoft VC++ for some time. Now I wanted to port my application to Mac OS X which offers gcc and the build fails. Here is the troublesome code: template<class T> class CList
3
2033
by: Ken Cecka | last post by:
This is a contrived example to demonstrate a syntax problem I'm struggling with: #include <vector> template <typename T> class Class { };
1
6454
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...
3
2696
by: wolverine | last post by:
Hi I am accessing a map from inside threads. There is a chance that an element is inserted into the map, from inside any thread. Since i don't know about thread safety of stl implementation i am using , i use mutex for thread safety. Now comes the question. Please answer this question assuming that i am not using a thread safe stl version. I obtain an iterator to the map from inside the critical section(protected by mutex). If i use...
5
2001
by: Bill Oliver | last post by:
Help! I am writing an image processing package. For one constructor, I allow creating an image from a map of points and color values. The points are of a "position" class and the colors are just a vector. It starts thusly: template <class T> image<T>::image(map<position<int, vector<T >& in_map){ cerr<< "***** ***** ***** ***** ***** *****\n";
1
6231
by: atomik.fungus | last post by:
Hi, I'm re-writting my matrix class to practice my programming and the computer doesn't let me compile the next code: ( this example come from the constructor of the class) //the matrix is made of vectors of vectors template< typename T > matrix< T >::matrix( unsigned rows, unsigned columns ) { vector< vector< T ::iterator p;
6
3130
by: antani | last post by:
VolumeType::ISetType::iterator findFirstIteratorMarked(const VolumeType::ISetType::iterator & it,const VolumeType::ISetType::iterator & e_it) { VolumeType::ISetType::iterator _it=it; while (_it!=e_it) { if ((*_it).mark) return _it; else _it++; }
3
1583
by: pnayak | last post by:
Hi, I am trying to implement an iterator to a List class. The code is included below. I get the following compiler error. At the point where I use a class that is defined inside a template (nList<T>::Link). The Link class is defined inside of a template. (I assume that is possible?). Any one know what I doing wrong? Many Thanks, Purush Error from Compiler (gcc 3.4.6) nList.cc:52: error: expected `;' before "current_" *** Error code 1
5
3129
by: maverik | last post by:
Hi all. I'm implementing class AbstractCollection - just a wrap on std::vector - to hide from clients the fact of using std::vector in the base (because in the future I may change vector to a list or another DS). So I try to do it in this way: template<typename T> class AbstractCollection { public:
0
9704
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
9571
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
10318
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 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...
0
9132
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...
1
7608
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 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...
0
6845
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();...
0
5505
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
4277
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
3
2976
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.