initialize a vector to zeros C++/C++11

后端 未结 2 1995
轻奢々
轻奢々 2020-12-04 07:45

I know in C++11 they added the feature to initialize a variable to zero as such

double number = {}; // number = 0
int data{};  // data = 0

相关标签:
2条回答
  • 2020-12-04 08:10

    Initializing a vector having struct, class or Union can be done this way

    std::vector<SomeStruct> someStructVect(length);
    memset(someStructVect.data(), 0, sizeof(SomeStruct)*length);
    
    0 讨论(0)
  • 2020-12-04 08:17

    You don't need initialization lists for that:

    std::vector<int> vector1(length, 0);
    std::vector<double> vector2(length, 0.0);
    
    0 讨论(0)
提交回复
热议问题