4

Why should I use iterators?

For example if I have code like this:

for (int i = 0; i < vec.size(); i++)
   cout << vec[i];

what would be the advantage of writing

for (vector<int>::iterator it != vec.begin(); it != n.end(); ++it)
   cout << *it;

Also, why is writing i < vec.size() and i++ more common in the first example and it != begin() and ++it more common in the second example? What is the difference how you increment it and why not always use an equal sign?

I understand iterators can be useful in C++11 range-based for loops and some STD algorithms, but why should I do it in normal code, since it is more verbose?

  • 1
    This should cover it: http://stackoverflow.com/questions/131241/why-use-iterators-instead-of-array-indices http://stackoverflow.com/questions/178934/iterators-why-use-them – chm Feb 26 '13 at 02:49
  • what if vec is a list? – billz Feb 26 '13 at 02:50
  • Neither is as good as `std::for_each(begin(vec), end(vec), [](int x) { std::cout << x; });`. Avoid off-by-one and other easy-to-inadvertently-make logic errors: use algorithms and iterator ranges instead of hand-written loops. – James McNellis Feb 26 '13 at 02:50
  • @James McNellis Why is it better? – Konstantin Kowalski Feb 26 '13 at 02:51
  • I've never seen anyone write this: `vector::iterator it != vec.begin()`; I'm fairly sure that's a compile-time error. – Nicol Bolas Feb 26 '13 at 03:05
  • "I understand iterators can be useful in C++11 range-based for loops and some STD algorithms, but why should I do it in normal code" - Why wouldn't range-Based for or algorithms not be considered normal code? – Grizzly Feb 26 '13 at 08:29

4 Answers4

2

Well not all containers have random access so you can do lookup on an index, so to normalize the interface iterators are fairly useful. Consider std::list. It doesn't support random access via a [] operator.

Taking this into account, to work across many heterogeneous types of containers, many STL functions like std::copy take iterators.

Doug T.
  • 64,223
  • 27
  • 138
  • 202
1

The point is that iterators allow you to iterate over anything that supports iterators in a generic fashion.

As for it being more verbose, the extra verbosity isn't horrible (and your example could be slightly improved using auto or using the C++11 range-based for loop) but that's really a stylistic issue.

Nik Bougalis
  • 10,495
  • 1
  • 21
  • 37
1

Lets say we have this code:

typedef std::vector<std::string> strings;

strings strs;
for( strings::const_iterator it = strs.begin(); it != strs.end(); ++it ) {
}

And later for watever reason we decide to switch to std::list. So we just replace typedef and code:

typedef std::list<std::string> strings;

strings strs;
for( strings::const_iterator it = strs.begin(); it != strs.end(); ++it ) {
}

Will work as before. But code with index variable will fail. Imagine what if you need to write a template code.

Slava
  • 43,454
  • 1
  • 47
  • 90
0

The tl;dr is that iterators work better in a general case for different kinds of objects (when for example the size() method may be slow).

If you want to read more about it:

Why use iterators instead of array indices?

Iterators.. why use them?

Community
  • 1
  • 1
chm
  • 1,519
  • 13
  • 21