Wednesday, 15 June 2011

c++ - std::vector: contiguous data and copy/move -



c++ - std::vector: contiguous data and copy/move -

i have 2 questions next code: 1) elements of faces contiguous? 2) std::vector re-create or move face f when inserting it?

#include <vector> int main() { struct face {}; std::vector<face> faces; (int i=0; i<10; ++i) { face f; faces.push_back (f); } homecoming 0; }

according standard § 23.3.6.1 class template vector overview [vector.overview] :

the elements of vector are stored contiguously, meaning if v vector<t, allocator> t type other bool, obeys identity &v[n] == &v[0] + n 0 <= n < v.size().

as far concerns sec question in prior c++11 compilers push_back re-create object force back.

after c++11 depends because push_back has 2 overloads, 1 takes lvalue reference , 1 takes rvalue reference.

in case copied because passing object lvalue. ensure motion of object utilize std::move().

faces.push_back(std::move(f));

c++ c++11 stdvector contiguous

No comments:

Post a Comment