Confusion with iterator

P

pandit

Hello i dont understand how to Deal with iterator. its little bit
confusing ?
how they work??
 
C

Christopher Pisz

pandit said:
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.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.
 
S

Salt_Peter

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.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.
 
P

pandit

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.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
 
R

red floyd

Christopher said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,536
Latest member
VeldaYoung

Latest Threads

Top