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?
std::iota= 1 possibility. that said, when you lack tool, create it.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.std::map<pieceList, Piece>and have theboardIdxbe a member of thePiece.std::map's value is actualPieces? Cause it doesn't really make sense to use a map to an index for astd::vector.