S
S
Sasha_882019-03-21 11:27:23
C++ / C#
Sasha_88, 2019-03-21 11:27:23

How to use iterators correctly? Is it possible to create 2 iterators per 1 container?

For example, there is a vector vector vec; with data. It is supposed to make dwarf sorting and therefore in the algorithm it is necessary to access the current element and the previous one at the same time when comparing. What is better to use: iterators or to have access to a vector element through an index, like an array (a[i - 1] < a[i]). I mean is it possible to have multiple iterators per container. In principle, I can organize all the additional checks when working with iterators. Or in this case indexes are better?
(The site has a bad captcha, it is difficult to understand clumsy letters, large or small)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Makarov, 2019-03-21
@Nipheris

I mean is it possible to have multiple iterators per container.

Of course you can. Invalidation of iterators depends on the type of container and the operations that you perform on the container, in the documentation for each of the container methods you can find out which iterators it invalidates and in what cases.
Using iterators will allow you to write an algorithm that works NOT ONLY with std::vector, but also with other containers that are subject to the requirements of your algorithm.

R
res2001, 2019-03-21
@res2001

2 iterators are possible. See an example of checking for polydrome here . True, there are lines and there are no changes to the container.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question