473,623 Members | 3,366 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Confusion with iterator

Hello i dont understand how to Deal with iterator. its little bit
confusing ?
how they work??
Dec 29 '07 #1
4 1334

"pandit" <ja*****@gmail. comwrote in message
news:82******** *************** ***********@d21 g2000prf.google groups.com...
Hello i dont understand how to Deal with iterator. its little bit
confusing ?
how they work??
iterators are for _iterating_ through a container. I wouldn't worry about
how they work, but I'd learn how to use them.
Just think of it as a "special" kind of pointer to an element in a
container. You do not know how the underlying implementation of the
container works, so they provide you with a way to access an element no
matter how it has been allocated.

Almost all stl containers have a begin() and end() method.

If you visualize your stl container like an array, then begin points to
element [0] and end points to the next element out of bounds.

example:

#include<vector >
#include <iostream>

int main()
{
std::vector<int bunchonumbers;

// Fill it up with numbers
for(int i =0; i < 5; i++)
bunchonumbers.p ushback(i);

// Visualize an array [0][1][2][3][4]

// Now you can _iterate_ through it

// begin points to element [0]
// end points to some memory after [4], containing something unknown
// notice how you can perform arithmetic just like pointers: it++
for( std::vector<int >::iterator it = bunchonumbers.b egin(); it !=
bunchonumbers.e nd(); it++)
{
// Notice how you can dereference the iterator just like a pointer
std::cout << *it;
}

return 0;
}

Iterators are just a special way of accessing elements in a container.
There is more to iterators than that, but it is the basics. For example,
there are different flavors of iterators with different rules, but that
comes later.
Dec 29 '07 #2
On Dec 29, 1:11 am, "Christophe r Pisz" <some...@somewh ere.netwrote:
"pandit" <jala...@gmail. comwrote in message

news:82******** *************** ***********@d21 g2000prf.google groups.com...
Hello i dont understand how to Deal with iterator. its little bit
confusing ?
how they work??

iterators are for _iterating_ through a container. I wouldn't worry about
how they work, but I'd learn how to use them.
Just think of it as a "special" kind of pointer to an element in a
container. You do not know how the underlying implementation of the
container works, so they provide you with a way to access an element no
matter how it has been allocated.

Almost all stl containers have a begin() and end() method.

If you visualize your stl container like an array, then begin points to
element [0] and end points to the next element out of bounds.

example:

#include<vector >
#include <iostream>

int main()
{
std::vector<int bunchonumbers;

// Fill it up with numbers
for(int i =0; i < 5; i++)
bunchonumbers.p ushback(i);

// Visualize an array [0][1][2][3][4]

// Now you can _iterate_ through it

// begin points to element [0]
// end points to some memory after [4], containing something unknown
// notice how you can perform arithmetic just like pointers: it++
for( std::vector<int >::iterator it = bunchonumbers.b egin(); it !=
bunchonumbers.e nd(); it++)
{
// Notice how you can dereference the iterator just like a pointer
std::cout << *it;
}

return 0;

}

Iterators are just a special way of accessing elements in a container.
There is more to iterators than that, but it is the basics. For example,
there are different flavors of iterators with different rules, but that
comes later.
Damn good explanation if you ask me.

Dec 29 '07 #3
On Dec 29, 11:11 am, "Christophe r Pisz" <some...@somewh ere.netwrote:
"pandit" <jala...@gmail. comwrote in message

news:82******** *************** ***********@d21 g2000prf.google groups.com...
Hello i dont understand how to Deal with iterator. its little bit
confusing ?
how they work??

iterators are for _iterating_ through a container. I wouldn't worry about
how they work, but I'd learn how to use them.
Just think of it as a "special" kind of pointer to an element in a
container. You do not know how the underlying implementation of the
container works, so they provide you with a way to access an element no
matter how it has been allocated.

Almost all stl containers have a begin() and end() method.

If you visualize your stl container like an array, then begin points to
element [0] and end points to the next element out of bounds.

example:

#include<vector >
#include <iostream>

int main()
{
std::vector<int bunchonumbers;

// Fill it up with numbers
for(int i =0; i < 5; i++)
bunchonumbers.p ushback(i);

// Visualize an array [0][1][2][3][4]

// Now you can _iterate_ through it

// begin points to element [0]
// end points to some memory after [4], containing something unknown
// notice how you can perform arithmetic just like pointers: it++
for( std::vector<int >::iterator it = bunchonumbers.b egin(); it !=
bunchonumbers.e nd(); it++)
{
// Notice how you can dereference the iterator just like a pointer
std::cout << *it;
}

return 0;

}

Iterators are just a special way of accessing elements in a container.
There is more to iterators than that, but it is the basics. For example,
there are different flavors of iterators with different rules, but that
comes later.
Thanks for it
Dec 29 '07 #4
Christopher Pisz wrote:
"pandit" <ja*****@gmail. comwrote in message
news:82******** *************** ***********@d21 g2000prf.google groups.com...
>Hello i dont understand how to Deal with iterator. its little bit
confusing ?
how they work??

iterators are for _iterating_ through a container. I wouldn't worry about
how they work, but I'd learn how to use them.
Just think of it as a "special" kind of pointer to an element in a
container. You do not know how the underlying implementation of the
container works, so they provide you with a way to access an element no
matter how it has been allocated.
[redacted]

I agree with Salt_Peter, that's a damned good explanation.

However, I'd like to say one thing to the OP, given your suggestion to
'think of it as a "special" kind of pointer".

To the OP: Remember, even though iterators behave like pointers,
ITERATORS ARE NOT POINTERS!!!! You *cannot* portably assume that they
are such.
Dec 29 '07 #5

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

Similar topics

38
3659
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...
4
1367
by: James Gregory | last post by:
I was having some issues with a program involving iterators and handles, and decided to try and make a short test program to try to work out where my problem was. However, my test program went wrong long before it resembled anything like my actual program. Here is the short test program: #include <iostream> #include <list>
26
1514
by: Michael Klatt | last post by:
I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: template <class Set> // Set is an instance of std::set<> class Iterator { public : typedef typename Set::value_type T; typedef typename Set::iterator SetIterator; Iterator(Set& container, const SetIterator& it);
8
2485
by: Rom | last post by:
I'm a bit confused as to how the STL set works with the <setname>.insert() and <setname>.find() functions Compiler used : CodeWarrior 5.0 My intial interpretation was that I needed to overload the '<' for the insert() and '==' operator for find() however it doesn't seem to be the case In fact it seems to only need to overload the '<' operator for BOTH functions (it simply ignores the '==' operator)
2
1403
by: wenmang | last post by:
Hi, all: I am reading the book "The C++ Stadnard Library" by Josuttis. I have trouble to understand operations for the insert iterator(page 272). ++itr, *itr and itr++ are defined as no-op, and "itr = value"(supported expression *itr = value") is defined as "inserts value" The example given in the book is: vector<int> coll; back_insert_iterator<vector<int> > itr(coll);
0
1891
by: A Taylor | last post by:
Hello, I am getting the following error using .NET 2003 and I wonder if anyone can help me understand what is going on. godcDoc.cpp(228) : warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\vector(621) : error C2440: 'initializing' : cannot
14
4867
by: shawnk | last post by:
I searched the net to see if other developers have been looking for a writable iterator in C#. I found much discussion and thus this post. Currently (C# 2) you can not pass ref and out arguments to an iterator method (one returning IEnumerable). I WOULD like to do this for transformative operations on a collection. I realize the need to lock target, etc. Does anyone know how to handle 'writable iterators' in C# 2?
3
3640
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules' that takes arguments of type (void *) because the ADT must be able to deal with any type of data. In my actual code, I will code the function to take arguments of their real types, then when I pass this pointer through an interface function, I...
0
2671
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never looked for one on the net (I did browse the boost library though). It doesn't matter right now, anyway. To put it simply, Abstract Iterator is mainly a wrapper class. It helps
0
8162
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
8662
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...
0
8603
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...
1
8317
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
8463
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
7134
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
5560
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
4154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2593
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

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.