Any guarantees with vector <bool> iterators?

cppreference says that iterators for specialization are vector<bool>defined as implementations, and many of them do not support features like ForwardIterator(and therefore RandomAccessIterator).

cplusplus adds the mysterious "most":

The types of pointers and iterators used by the container are not necessarily pointers nor the corresponding iterators, although they will mimic most of the expected behavior.

I do not have access to the official specification. Are there any iterative behaviors guaranteed for iterators vector<bool>?

More specifically, how to write standard compatible code to insert an element in the middle vector<bool>? The following works on several compilers I tried:

std::vector<bool> v(4);
int k = 2;
v.insert(v.begin() + k, true);

Will it always be?

+4
source share
2 answers

The main problem with iterators vector<bool>is that they are not ForwardIterators. C ++ 14 [forward.iterators] / 1 requires the ForwardIterators type to be reference, T&or const T&, if necessary.

Any function that accepts an advanced iterator in a range Tcan do this:

T &t = *it;
t = //Some value.

vector<bool> reference bool&; -, bool. bool, bool. , :

bool &b = *it;

lvalue , -. .

, vector<bool> , ForwardIterators .

. , vector<bool>, , , .

, RandomAccessIterators, , (. ). , ..

vector<bool> , vector, bool s. , vector<bool> , .

, vector<bool> std::sort.

+2

++ 14 [vector.bool]/2:

, vector, , bool allocator_traits::construct (20.7.8.2) .

+3

Source: https://habr.com/ru/post/1651891/


All Articles