What is difference between iterators and pointers

2020-06-28 02:42发布

问题:

So I wrote this program and it works, but I am being told that I need to use iterators to move about in the functions that I wrote. I am currently using pointers but I thought they were basically the same things. And if there is a difference, how is an iterator used?

UPDATE: So what I am understanding is that an iterator, unlike a pointer, will move to the next item in a list even if that item is not next sequentially in memory? Is this correct? But then how does it know where to move? I have programmed a binary tree that adds and deletes nodes. In each node there are pointers that point to the two children (or just one or none) and also a pointer that points to the parent. In the functions I recursively call the functions that I am using and passing the new pointers. I have a feeling that replacing what I have with iterators should be easy, but I am still unsure.

update 2: I have been doing some more reading and trying some examples and now I am confused on how to even declare an iterator. Everything I come across acts like I need to make another class just for the iterator. But when I try all I get is a list of compiler errors. Any help would be appreciated. Thanks

回答1:

There is a difference, consider:

int arr[] = { 5, 6, 7, 8, 9, 10 };
int * ptr = arr;

printf("%d\n", *ptr++);
printf("%d\n", *ptr++);

You might look at that and say, "why use an iterator, what's the difference?". In this example we know we can just point to the base address, and step through by incrementing the pointer(which will step sizeof(int) bytes). Using an iterator seems silly because it's just wrapping walking a pointer.

The key is, it's just wrapping walking a pointer in this case.

What if the underlying data wasn't contagiously allocated - you can't just increment a pointer anymore. What if it's a tree? What if it's a linked list?

The point of the iterator concept is that you can abstract the concern of how to walk a collection, and just rely on the standard exposed iterator methods. For users of your collection, they don't have to have any insight into how your collection stores elements.



回答2:

Iterators are opaque objects that allow to browse through a certain collection of objects in a unified way (always the same way), even though the collections may have a very different internal structure. For example: a std::map is usually implemented as a red-black tree, std::vector is an array occupying a block of memory. Going through all of the elements in vector is very different than going through all of the elements in a tree. Iterators abstract it for you - you always do it the same way (using ++ and -- operators and iterators):

auto iter = <whatever-container>.begin();
auto end  = <whatever-container>.end();
//go through all elements of the container:
while(iter != end)
{
    //do something
    ++iter;
}

Certain C++ compiler optimizations boil down to use pointers (e.g. std::string iterators usually would be compiled to just pointers to characters with optimizations turn on).



回答3:

Even if you choose iterator or pointer, the result is same.

But iterator is abstract method and design pattern. It's adapted a lot of language because it's how to design. It's useful because it's well known by many people. So many people can understand easily.