1

I have a question about the preference between iterator and subscripting. For example, if I am doing something like:

for (vector<int>::size_type i = 0; i != ivec.size(); ++i) {
  ivec[i] related operation
}

for (vector<int>::iterator it = ivec.begin(); it != ivec.end(); ++it) {
  *it related operation
}

Which one is preferred? In the perspective of: 1. Performance 2. Clarity 3. Other concerns?

I am aware this question have been previously discussed link. But in the other post it only talked about the performance cost of size();

Suppose this is about vector, and the cost of size() is negligible.

what about cost of using subscript vs. iterator?

Thanks.

Community
  • 1
  • 1
JASON
  • 7,371
  • 9
  • 27
  • 40
  • @Rapptz I am not very satisfied with answer there – JASON Mar 01 '13 at 05:51
  • 3
    @AlanShore There are 24 answers there, surely one meets your standards. – Rapptz Mar 01 '13 at 05:53
  • 3
    Playing devil's advocate, similar questions have been asked [15 times before](http://stackoverflow.com/search?q=%5Bc%2B%2B%5D+iterator+vs+indices). – Rapptz Mar 01 '13 at 05:56

1 Answers1

1

Perfomance: with vector in most cases these cases are equal, since vector<T>::iterator is really pointer to T in most cases.

Clarity: work with iterator.

ForEveR
  • 55,233
  • 2
  • 119
  • 133