Hello. I'm new to C++ and I just learned how to use vectors and lists and indices and iterators, but i'm confused about iterators. Mainly, why should I use them instead of indices? They seem to add more complexity to accomplish the same thing as using indices... so could someone please clarify for me?

Recommended Answers

All 2 Replies

When using list, you have to use iterators. When using vectors you
have a choice of using index or iterators, usually you should pick the index
version( which means use the operator [] ). The STL uses iterators
to generalize their functions. For example using std::sort(..), it takes in
a begin and a end iterator. That means that the algorithm stl uses does not have
make different algorithms for different containers.

Can you use an index to access a list?

Can an iterator ever be out of bound on a vector?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.