Looking for an easy way to reinitialize a struct

后端 未结 2 1594
太阳男子
太阳男子 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")}
    };
    
    0 讨论(0)
  • 2021-01-24 08:16

    if you add a simple constructor:

    struct CoolStruct
    {
        CoolStruct(int id, uint32 type, uint32 subtype, String name) : id(id), type(type), subtype(subtype), name(name) {}
        int id;
        uint32 type;
        uint32 subtype;
        String name;
    };
    

    you can then do this:

    CoolVector.push_back(CoolStruct(1, EQData::EQ_EFFECT_TYPE_PARAMETRIC, 0, T("Parametric")));
    CoolVector.push_back(CoolStruct(2, EQData::EQ_EFFECT_TYPE_FILTER_LOW_PASS,EQData::EQ_FILTER_TYPE_FILTER_BUTTERWORTH_12DB, T("Low Pass")));
    
    0 讨论(0)
提交回复
热议问题