Setting vector elements in range-based for loop

后端 未结 4 1319
南旧
南旧 2020-11-28 16:50

I have come across what I consider weird behaviour with the c++11 range-based for loop when assigning to elements of a dynamically allocated std::vector. I hav

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-28 17:22

    The reason your loop fails is because you reference vector elements by value. However, in this case you can eliminate the loop altogether:

    dataInput.read(reinterpret_cast(CTdata->data()), arraySize*sizeof(short int));
    

    This reads the content into the vector in a single call.

提交回复
热议问题