473,397 Members | 2,068 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,397 software developers and data experts.

Confusion with iterator

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

"pandit" <ja*****@gmail.comwrote in message
news:82**********************************@d21g2000 prf.googlegroups.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<intbunchonumbers;

// Fill it up with numbers
for(int i =0; i < 5; i++)
bunchonumbers.pushback(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.begin(); it !=
bunchonumbers.end(); 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, "Christopher Pisz" <some...@somewhere.netwrote:
"pandit" <jala...@gmail.comwrote in message

news:82**********************************@d21g2000 prf.googlegroups.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<intbunchonumbers;

// Fill it up with numbers
for(int i =0; i < 5; i++)
bunchonumbers.pushback(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.begin(); it !=
bunchonumbers.end(); 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, "Christopher Pisz" <some...@somewhere.netwrote:
"pandit" <jala...@gmail.comwrote in message

news:82**********************************@d21g2000 prf.googlegroups.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<intbunchonumbers;

// Fill it up with numbers
for(int i =0; i < 5; i++)
bunchonumbers.pushback(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.begin(); it !=
bunchonumbers.end(); 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**********************************@d21g2000 prf.googlegroups.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
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...
4
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...
26
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...
8
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...
2
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...
0
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...
14
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...
3
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'...
0
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...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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
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,...
0
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...

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.