eg: vector<int> listOfValues
Hi,
Vectors can be accessed by index like listOfVaules[9]
OR
by iterator.
which is the best way of accessing? is accessing by index a bad practice?
Thanks,
Rahul
eg: vector<int> listOfValues
Hi,
Vectors can be accessed by index like listOfVaules[9]
OR
by iterator.
which is the best way of accessing? is accessing by index a bad practice?
Thanks,
Rahul
No, it is not bad practice.
Better practice might be to use at() instead of operator[] with an index to help avoid using invalid indexes. You should be fine with any of those options, though.
Note that depending on the situation, using an index or using an iterator might be more clear to a reader of the code than the other option. Often that's the most important factor in your choice.