0

Say I have an array in a class and I have to write a member function for that class that places a new value in the middle of the array based on an iterator that is passed in.

For example:

void insert(iterator itr, int value)

If i was allowed to use integers for indexing then I could just do:

array[i] = value;

Is there a way to do this same thing with iterators?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
LoneWolf
  • 35
  • 8

2 Answers2

0

The insert() operation for standard c++ container classes implies that the size of the container will be changed, while the indexing operator relies on already available indices, or creates a new one for std::set or std::map like container classes.

So these operations cannot be compared (made the same) in general.

If i was allowed to use integers for indexing

An iterator is an abstract concept, and the implementation might be more complicated than just transpose it to an integer value used as index for a contiguous array.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
-1

You ca just add i to the iterator. They behave just like pointers a clearer esplanation is given on this question

Community
  • 1
  • 1
Pedro Henrique
  • 601
  • 6
  • 17