Looking for an easy way to reinitialize a struct

后端 未结 2 1593
太阳男子
太阳男子 2021-01-24 07:43

I have a struct called CoolStruct:

struct CoolStruct

{
    int id;
    uint32 type;
    uint32 subtype;
    String name;
};

I have a vector of

2条回答
  •  清酒与你
    2021-01-24 07:58

    how about:

    CoolStruct t1 = {1, EQData::EQ_EFFECT_TYPE_PARAMETRIC, 0, T("Parametric")};
    coolVector.push_back(t1);
    CoolStruct t2 = {2, EQData::EQ_EFFECT_TYPE_FILTER_LOW_PASS,EQData::EQ_FILTER_TYPE_FILTER_BUTTERWORTH_12DB, T("Low Pass")};
    coolVector.push_back(t2);
    

    In C++0x, I think you should be able to do:

    CoolStruct t;
    t = {1, EQData::EQ_EFFECT_TYPE_PARAMETRIC, 0, T("Parametric")};
    coolVector.push_back(t);
    t = {2, EQData::EQ_EFFECT_TYPE_FILTER_LOW_PASS,EQData::EQ_FILTER_TYPE_FILTER_BUTTERWORTH_12DB, T("Low Pass")};
    coolVector.push_back(t);
    

    or even:

    coolVector.push_back({1, EQData::EQ_EFFECT_TYPE_PARAMETRIC, 0, T("Parametric")});
    coolVector.push_back({2, EQData::EQ_EFFECT_TYPE_FILTER_LOW_PASS,EQData::EQ_FILTER_TYPE_FILTER_BUTTERWORTH_12DB, T("Low Pass")});
    

    In fact, if you really want to get creative (and you don't have any previous elements that you want to keep), you can replace the whole vector with this syntax:

    coolVector = {
       {1, EQData::EQ_EFFECT_TYPE_PARAMETRIC, 0, T("Parametric")},
       {2, EQData::EQ_EFFECT_TYPE_FILTER_LOW_PASS,EQData::EQ_FILTER_TYPE_FILTER_BUTTERWORTH_12DB, T("Low Pass")}
    };
    

提交回复
热议问题