Thread: Iterator vs. pointer...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    36

    Iterator vs. pointer...

    hey all,

    i was just wondering:
    What is the difference between iterator and pointer?

    please help..

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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.

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    36
    ummm..
    i did not get you...
    please explain in simpler terms..
    <a request>

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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.
    Right 98% of the time, and don't care about the other 3%.

    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.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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"

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Pointer is a subset of Iterator, put mathematically.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access via iterator to pointer container
    By atlmallu in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2009, 01:13 PM
  2. Pointer to List Iterator As Function Argument
    By bengreenwood in forum C++ Programming
    Replies: 8
    Last Post: 06-17-2009, 05:30 AM
  3. Normal iterator pointer behavior
    By SevenThunders in forum C++ Programming
    Replies: 9
    Last Post: 04-01-2008, 12:11 PM
  4. Connecting input iterator to output iterator
    By QuestionC in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2007, 02:18 AM
  5. std::map::iterator
    By Magos in forum C++ Programming
    Replies: 2
    Last Post: 02-03-2006, 07:47 PM