hey all,
i was just wondering:
What is the difference between iterator and pointer?
please help..
hey all,
i was just wondering:
What is the difference between iterator and pointer?
please help..
They lie on different layers of abstraction.
Pointers can point to an address and knows about next and previous addresses with some pointer arithmetic.
Iterators generalize the concept of iterating over something and may be built on top of pointers.
ummm..
i did not get you...
please explain in simpler terms..
<a request>
Sorry..I have no idea how to explain iterators in a simpler manner.
Here is a naive example of how to implement iterators.
https://www.pastee.org/cjwsp
To understand what an iterator is, it is first necessary to understand what a container is. A container is something that contains an arbitrary number of elements of some type in an organised manner. Examples of containers include a vector, a list, a queue, a string, or even an array.
An iterator is any object that allows a programmer to access all elements of a container sequentially (one after the other). An iterator is obtained from a container in some manner, and used to access elements of that container.
A pointer is a variable that contains the address of some other variable. A pointer is a type of iterator, since a pointer can be used to access all elements of a container sequentially.
Pointer is to Iterator, as Knife is to Weapon.
I.e. there's a "can be used as a" relationship.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
A pointer is an iterator to a primitive array. But it can also be used as a null-able reference (a reference to something or null). Grumpy explained what an iterator is.
Now with C++11, the std::array container should be preferred to a primitive array, so you don't need to use pointers as iterators directly. (Iterators to std::vector and std::array are often implemented as primitive pointers, but you don't know that without looking at their implementation)
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.