C++初始化类的成员数组的所有值

use*_*252 3 c++ arrays initialization class

在C++中如何初始化类的成员数组的所有值?

#define MAX_MATRIX 20
Class Matrix {
public:   
         Matrix(); //constructor
protected:
         int n[MAX_MATRIX];   // note cannot do = { 0} or w/e here
};

Matrix::Matrix()
{
     // how to set all n to -1?
}
Run Code Online (Sandbox Code Playgroud)

R. *_*des 6

你可以使用std::fill:

std::fill(begin(n), end(n), -1);
Run Code Online (Sandbox Code Playgroud)

(这些beginend函数可以std在C++ 11 中的命名空间中找到,或者您可以在C++ 03中自己轻松实现它们)