How do I initialize vector/array using an enum?
One way to solve this would be adding a value to the enum that is always the last one. Then you could fill the vector by looping up to the value. Something like this:
enum VALUES{
VALUES_FIRST = 0,
VALUES_SECOND,
VALUES_END
};
std::vector<VALUES> Allvalues;
for(int i = 0; i < VALUES_END; i++){
Allvalues.push_back(static_cast<VALUES>(i));
}
Would fill the vector with all the values in the enum (not including the last marker value) as long as you don't put anything after VALUES_END
.
So if you want to generate a range from 1 to 32 you can use generate to do that, combined with a lambda.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> v(32);
int n=0;
std::generate(v.begin(), v.end(), [&]{ return ++n; });
//to display the results
for (auto& it: v){
cout<<it<<" ";
}
return 0;
}
Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
Hope that helps