gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 19:46:38 32 4
gpt4 key购买 nike

在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?
}

最佳答案

你可以使用std::fill:

std::fill(begin(n), end(n), -1);

(这些 beginend 函数可以在 C++11 的命名空间 std 中找到,或者您可以轻松地在 implement them yourself 中找到C++03)

关于C++ 为类初始化成员数组的所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9073661/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com