When initializing the vector, can you do it like an array initialization?

Asked 2 years ago, Updated 2 years ago, 90 views

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);
...

c++ vector initialization

2022-09-22 15:51

1 Answers

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]) );


2022-09-22 15:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.