Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you initialise a vector of structures that contain vectors as members?

Tags:

c++

stl

I'm using Boost::assign to initialise a vector of structures as follows:

const std::vector<spriteInfo> Resources::hudObjectInfo = boost::assign::list_of
    ( spriteInfo(FALSE, SCORE_START_X, SCORE_START_Y, CENTER_ALIGN ))
    ( spriteInfo(FALSE, SCORE_START_X+NUM_DIFF, SCORE_START_Y, CENTER_ALIGN))
    ... /* and so on */
;

NB. The spriteInfo struct currently looks like this:

struct spriteInfo
{
    spriteInfo::spriteInfo(bool i, float x, float y, int align):
        invisible(i), x1(x), y1(y), alignment(align){}
    bool        invisible;
    float       x1;
    float       y1;
    int         alignment;
};

However, I'd like to make a std::vector<int> as a member of spriteInfo.

If I do that, how would the assignment above look? i.e. Can you initialise a vector whilst passing it as a parameter to a constructor?

like image 330
BeeBand Avatar asked Dec 05 '25 15:12

BeeBand


1 Answers

This should do :

struct spriteInfo
{
    spriteInfo(bool i, float x, float y, int align, const std::vector<int> &v):
        invisible(i), x1(x), y1(y), alignment(align), something(v) {}
    bool             invisible;
    float            x1;
    float            y1;
    int              alignment;
    std::vector<int> something;
};

int main()
{
    const std::vector<spriteInfo> Resources::hudObjectInfo = boost::assign::list_of
        ( spriteInfo(FALSE, SCORE_START_X, SCORE_START_Y, CENTER_ALIGN, boost::assign::list_of(1)(2)(3)))
        ( spriteInfo(FALSE, SCORE_START_X+NUM_DIFF, SCORE_START_Y, CENTER_ALIGN, boost::assign::list_of(4)(5)(6)))
    ;
}
like image 147
icecrime Avatar answered Dec 09 '25 20:12

icecrime