2

In my code the main data structure is

std::vector<std::vector<T>> Worldlines ;

In one of my subroutines I remove and add elements (potentially causing a reallocation of the container to have more capacity) to one of the components (let's say Worldlines[i]).

If I had some T in the vector Worldlines[i] whose positions I saved as std::vector<T>::iterator objects, they might in general be invalidated if Worldlines[i] is reallocated.

What about iterators pointing to the T that belong to the Worldlines[j] with j != i ? Are they guaranteed to be still valid, or the reallocation of one of the vectors may cause reallocations in the others, since they are bound in a vector of vectors ?

Thanks everyone.

aa24kk
  • 445
  • 4
  • 9

2 Answers2

2

They will remain valid.

Even if a vector is reallocated other vectors in the same container are unaffected.

Basically you can image a vector implemented as:

struct Vector {
    T * begin_of_memory;
    int number_of_allocated_elements;
    int number_of_used_elements;
};

and iterators as being just T*.

When the vector needs to be resized to make room for new elements of course begin_of_memory will change and also any iterators currently in the middle of the area will be no more usable.

But the Vector structure itself only changed its contents and pointers to it are still valid.

6502
  • 112,025
  • 15
  • 165
  • 265
  • I suppose this is because of the arguments shown in http://stackoverflow.com/questions/19504455/would-vector-of-vectors-be-contiguous and http://stackoverflow.com/questions/10898007/stdvector-of-stdvectors-contiguity. I just wanted to be very sure. Thanks. – aa24kk Aug 08 '15 at 10:42
  • The standard says this (23.3.6.5): If no reallocation happens, all the iterators and references before the insertion point remain valid. – Vinay Shukla Aug 08 '15 at 10:48
  • @VinayShukla How is that relevant here? "When the vector needs to be resized" means reallocation happens. – juanchopanza Aug 08 '15 at 10:53
  • In a `std::vector>`, reallocating one of the internal vectors may cause the others to be reallocated as well, or are they completely indipendent ? – aa24kk Aug 08 '15 at 12:42
  • @AdrianoAngelone: They're independent. In C++ each object has a fixed size in memory... variable sized containers keep their content in other areas, not inside the object itself. – 6502 Aug 08 '15 at 13:31
2

All iterators of the external vector including the iterator that points to the element Worldlines[i] are valid. You simply changed the value of the object pointed to by the iterator that corresponds to the position of the element Worldlines[i] The vector was not reallocated because neither operation that requires a reallocation was done with the vector.

Also iterators of all elements of the vector are also valid except the iterators of the element Worldlines[i] itself elements of which might be reallocated.

You can imagine the situation like you have an array of pointers (in fact a vector of vectors is a dynamically allocated array of pointers). If you changed the value of some pointer of an element of the array the array itself was not reallocated and the changed element still have the same index in the array.:)

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    @Vinay Shukla And what? – Vlad from Moscow Aug 08 '15 at 10:48
  • I guess by 'iterators of all elements of the vector' you mean all iterators to elements of `Worldlines[j]` where j != i. – aa24kk Aug 08 '15 at 10:56
  • @Adriano Angelone I hoped that I wrote clear.:) Forget about the value type of the vector. You can imagine the vector like std::vector. If you changed the value of type T of some element of the vector the vector itself was not changed: neither element was added or deleted. So all iterators of the vector are valid. – Vlad from Moscow Aug 08 '15 at 10:59
  • You wrote clear, actually I didn't :) I was actually worried about iterators to the `T` inside the `Worldlines[j]`, not to iterators to the vectors themselves. – aa24kk Aug 08 '15 at 11:02
  • @Adriano Angelone If some operations were applyed to the vector Worldlines[j], that can result in the reallocation of its elements then its iterators are invalid. – Vlad from Moscow Aug 08 '15 at 11:06
  • So if I operate on only one of the `Worldlines[i]`, the iterators to the `T` inside him may be invalidated, but all those to the `T` inside the other `Worldlines[j]` are guaranteed to be valid. – aa24kk Aug 08 '15 at 11:08
  • @Adriano Angelone And all iteartors that point to all elements Worldlines[k] where k can be equal to i also are valid., – Vlad from Moscow Aug 08 '15 at 11:11