Personally, I really like the C ++ 2011 approach, because it does not require the use of sizeof() and I donβt remember setting the boundaries of the array if you ever change the boundaries of the array (and you can define the corresponding function in C ++ 2003, if you also want to):
#include <iterator> #include <vector> int x[] = { 1, 2, 3, 4, 5 }; std::vector<int> v(std::begin(x), std::end(x));
Obviously, with C ++ 2011, you might want to use initializer lists:
std::vector<int> v({ 1, 2, 3, 4, 5 });
Dietmar KΓΌhl Jan 08 '12 at 3:50 2012-01-08 15:50
source share