Vector manipulation in C ++


Is there any problem if I go through a vector by taking its size with vector.size () and then make a for on the vector?

I'm asking this question because almost every time I see vectors manipulation, an iterator is used to traverse it and get its values, and I, for being a beginner, I do not understand the difference (advantages and disadvantages) between one and another.

asked by anonymous 06.02.2016 / 04:39

1 answer


Specifically for type vector iterate with for normal controlling access is faster than through an iterator. But this is not true in most other existing data collections. You have to measure.

To access the element by index, the collection must have a way to do this, it may not have or may be slow, only the iterator will work or work better. vector obviously can access the index and it's pretty fast.

The main reason for using an iterator is to give better abstraction, to demonstrate in the code that you are not just accessing elements through your index, but traversing it from beginning to end or vice versa. So the iterator, in vector , may be preferred because it is more readable, or at least more concise. It also helps the fact that in other cases it is better to use iterator because only those will stop using iterator? Standardized.

It will one day need to change from vector to another collection, if you write the code properly, few changes already solve the problem in a practical way. If you type for normal and the new collection that will replace vector does not go well with this form, or you will have to modify things, or worse, you will not have to change anything, but the result will be the same, but you will have appointments (performance for example).

You can assume things in for normal that may not be true. For example, it is common to use int to increase the index. Is it the most appropriate? When you choose the most concrete, you have to take a lot of care. Of course choosing the abstract needs others. In concrete you need to know more about the collection used. Which may be good in some scenarios, though it is worse in most. You have to use whatever is most appropriate in each situation. Concrete can often be considered premature optimization .

Of course access through the index allows you to "sweep" vector in less usual ways, although you have some control over the iterator as well.

The iterator can give you a bit of security or create a problem. It will depend on what will be done in the body. It has operations that will not be possible naturally using an iterator. But they can be possible if you modify something, such as using a reference. Some operations that can be disastrous or at least harmful can be done without the programmer noticing, for example incrementing the iterator can create a new temporary iterator that is slower than normal.

The iterator may be shorter:

for (auto& elemento: vetor)

Some prefer to use even ready-made functions to iterate more abstractly yet. The std::algorithms is your friend. The more abstract you tend to have less bugs .

06.02.2016 / 11:54