Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

C++ STL: Copy vector to array

📅 2010-May-25 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ algorithms, arrays, copy, cpp, stl, vector ⬩ 📚 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 );

© 2023 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘📧