Obtaining item index in ranged based for on vector

前端 未结 2 979
别那么骄傲
别那么骄傲 2020-12-11 15:48

The C++11 introduced ranged-based for loop that is internally implemented using (const) iterators so this:

std::vector vec;

for(std::stri         


        
2条回答
  •  时光说笑
    2020-12-11 16:17

    Yes, that's a valid solution. The underlying data is guaranteed to be contiguous (std::vector is supposed to be a dynamic array, more or less).

    n4140 §23.3.6.1 [vector.overview]/1

    The elements of a vector are stored contiguously, meaning that if v is a vector where T is some type other than bool, then it obeys the identity &v[n] == &v[0] + n for all 0 <= n < v.size()

提交回复
热议问题