int a[] = {1,2,3,4,5};
Like this.
std::vector<int> ints = <1,2,3,4,5>;
Is there a way to do it like this?
I don't want to use the method I know
Please let me know if there is an easy and intuitive way
std::vector<int> ints;
ints.push_back(1);
ints.push_back(2);
...
Compilers support C++11Cotton
std::vector<int> v = {1, 2, 3, 4, 5};
It's also possible. However, it only works with GCC 4.4 and later
If you can't do the above, the alternative is There is a way to initialize the vector using an array. You can write it like this.
static const int arr[] = {16,2,77,29};
vector<int> vec (arr, arr + sizeof(arr) / sizeof(arr[0]) );
© 2024 OneMinuteCode. All rights reserved.