How to initialize an array of struct in C++?

前端 未结 1 1999
南笙
南笙 2020-12-03 06:29

I have the following struct in my C++ code (I am using Visual Studio 2010):

struct mydata
{
    string scientist;
    double value;
};


        
相关标签:
1条回答
  • 2020-12-03 07:03

    The syntax in C++ is almost exactly the same (just leave out the named parameters):

    mydata data[] = { { "Archimedes", 2.12 }, 
                      { "Vitruvius", 4.49 } } ;
    

    In C++03 this works whenever the array-type is an aggregate. In C++11 this works with any object that has an appropriate constructor.

    0 讨论(0)
提交回复
热议问题