8

I am wondering if there are some way to initialize a vector using an enum. The enum is necessary because I am creating a vector of objects (same class, Chess_piece, but different type). I want to be able to access the element without a lot of tests (if (this is white pawn 8)...). The enum can be used to itemize the pieces in a nice way vec(W_PAWN8).... Anyway when I create vector I do something like this (pseudo code)

//generate enum of pieces
enum pieceList{
...
} pieceEnum;
vector<int> pieceIter = {W_PAWN1,W_PAWN2,...}; //equal to {1,2,...}
//board index goes from lower left to upper right
vector<int> boardIdx = {8,9,10,11,12,13,14,15,16,1...};
vector<Piece*> pieceVec;
for (int i=0; i<32; i++)
    pieceVec.pushback( new Piece( boardIdx(i), pieceIter(i) ) );

However, now I actually write the same thing 2 times. Both when I create the enum and pieceIter. For this program I can live with it, but I may have the same issue more than once.

This is why I wonder, does it exist something like vector<int> pieceIter {pieceEnum}; in c++? The code snippet in the previous sentence is invalid of course, but I think it hints my problem, to use all variables in the enum and initialize the vector in a simple way?

If not, is it possible to use some kind of "range initialization" for vector like in matlabl Something like:

vector<int> vec {1:32};

But with c++ syntax?

9
  • std::iota = 1 possibility. that said, when you lack tool, create it. Commented Dec 17, 2014 at 18:17
  • @Cheersandhth.-Alf That is of course a possibility, I did not know about std::iota. Thanks! I guess that the other way, initializing a vector/array with an enum may require some more work. And possibly support from the compiler. Commented Dec 17, 2014 at 18:34
  • @patrik This isn't an attempt to answer the question, but this would make more sense to me as a std::map<pieceList, Piece> and have the boardIdx be a member of the Piece. Commented Dec 17, 2014 at 18:49
  • possible duplicate of Set std::vector<int> to a range Commented Dec 17, 2014 at 18:50
  • 1
    @patrik Welp, I did this to you. Sorry StackOverflow is so hateful of late. Another note on the context here though: I assume your std::map's value is actual Pieces? Cause it doesn't really make sense to use a map to an index for a std::vector. Commented Dec 17, 2014 at 20:28

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I see, that would work for a enum increasing with steps of 1, starting at 0 at least. However, I guess that you mean i <= VALUES_LAST or i<VALUES_LAST+1. I can accept that and i you also have a solution for a general enum, where the values doe not need to increase with steps of 1 I would appreciate it.
As far as I know C++ doesn't store the enum values so that you could retrieve them at runtime making it impossible to iterate over the enum values.
3

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

1 Comment

Nice! I guess that I will make a function that does this.
0

You can also use the vectors constructor to initialize the vector like follows:

enum MyEnum {
  FIRST,
  SECOND,
  THIRD
};

// Example 1:
vector<int> v1(4, MyEnum::FIRST); // init a vector of size 4 to "FIRST".

// Example 2:
vector<MyEnum> v2(4, MyEnum::SECOND); // int a vector of enums.

// Example 3
vector<int> v3 {MyEnum::FIRST, MyEnum::THIRD}; // vector of int of size 2 with different values.

// Example 4:
vector<MyEnum> v4 {MyEnum::FIRST, MyEnum::THIRD}; // vector of enum.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.