I have no idea how to explain iterators in a simpler manner. Here is a naive example of how to implement iterators. 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. If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it.
Suck it up, Buttercup, and read this , this , and this before posting again. Pointer is to Iterator, as Knife is to Weapon. My homepage Advice: Take only as directed - If symptoms persist, please see your debugger Linus Torvalds: "But it clearly is the only right way. Simple difference: C does not have iterators.
So then how would I use an iterator to replace the pointers that I have? Why do you need to use iterators if your program works already? An iterator is an abstraction of the pointer concept with pointer-like semantics. Whereas a pointer can iterate over contiguous blocks of memory, iterators can be made to iterate over complex, arbitrarily arranged data structures.
Add a comment. Active Oldest Votes. The key is, it's just wrapping walking a pointer in this case. Improve this answer. Nick 6, 16 16 gold badges 36 36 silver badges 44 44 bronze badges. Walter Delevich Walter Delevich 1 1 silver badge 8 8 bronze badges. What you'll want to do is create a class that inherits from std::iterator, that class will then define methods that describe how to walk your collection.
See this link for more an example: codereview. Notice how they embed an iterator class that extends the std::iterator class with knowledge of how to walk his specific container. Even if you choose iterator or pointer, the result is same. Takuya Fujiwara Takuya Fujiwara 75 4 4 bronze badges. The Overflow Blog. Does ES6 make JavaScript frameworks obsolete? Podcast Do polyglots have an edge when it comes to mastering programming Featured on Meta. Now live: A fully responsive profile.
Linked Improve Article. Like Article. Note that the program. Recommended Articles. Difference between constant pointer, pointers to constant, and constant pointers to constants. Article Contributed By :. Easy Normal Medium Hard Expert.
Writing code in comment? Please use ide.
0コメント