Agnel Kurian Agnel Kurian - 1 year ago 92
C++ Question

What is the easiest way to initialize a std::vector with hardcoded elements?

I can create an array and initialize it like this:

int a[] = {10, 20, 30};

How do I create a
and initialize it similarly elegant?

The best way I know is:

std::vector<int> ints;


Is there a better way?

Answer Source

One method would be to use the array to initialize the vector

static const int arr[] = {16,2,77,29};
vector<int> vec (arr, arr + sizeof(arr) / sizeof(arr[0]) );
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download