Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

C++ STL: Copy vector to array

📅 2010-May-25 ⬩ ✍️ Ashwin Nanjappa ⬩ 📚 Archive

The array behaves like a vector and so can be used almost everywhere a vector is used. So, a vector can be copied into an array using std::copy algorithm. But, make sure that the array is big enough to hold the elements of the vector when you do this:

#include <vector>

std::vector<Foo> fooVec;
Foo fooArr[FOO_MAX];

std::copy( fooVec.begin(), fooVec.end(), fooArr );