简体   繁体   English

如何初始化或更改类中数组内的值?

[英]How to initialize or change values inside of an array in a class?

I am working with a class which has an array of characters. 我正在使用一个具有一系列字符的类。

class board
{
   char spot[64];

public:
   board();
   ~board(void);


};

I want to set each of the values in the array to the same character when the program begins how can I initialize there values in the constructor? 我想在程序开始时将数组中的每个值设置为相同的字符,如何在构造函数中初始化值? and is there an easy way to access and allow changes to them by their index? 是否有一种简单的方法来访问并允许通过索引更改它们?

Thank you, 谢谢,

You cannot do this in C++03. 你不能在C ++ 03中这样做。 However, it is possible to initialize arrays in a constructor initializer list in C++11: 但是,可以在C ++ 11中的构造函数初始化列表中初始化数组:

board::board()
: spot { 1, 2, 1, 2, /* ... */ }
{  }

You can either type out the values, or come up with a magic variadic template to provide "N repeated values" (as long as the type is integral). 您可以输入值,或者提出一个魔术可变参数模板来提供“N个重复值”(只要类型是整数)。 Also, any missing elements are zero-initialized (eg char spot[4] { 1, 2 }; ). 此外,任何缺失的元素都是零初始化的(例如char spot[4] { 1, 2 }; )。

For access, you use spot[i] inside the class, and you can write suitable accessor functions if you need to (though you should always worry if your class is just "forwarding" a member -- really you want your class to encapsulate some higher-order functionality). 对于访问,你在类中使用spot[i] ,如果需要你可以编写合适的访问器函数(尽管你应该总是担心你的类只是“转发”一个成员 - 你真的希望你的类封装一些更高阶的功能)。

If you want to see template code to "initialize an array of N with fixed values", perhaps you should post that as a separate question. 如果你想看模板代码“初始化一个具有固定值的N数组”,也许你应该把它作为一个单独的问题发布。


Update: Here is a naive template trick that initializes all array elements to the value 2 . 更新:这是一个天真的模板技巧,将所有数组元素初始化为值2

#include <iostream>
#include <utility>
#include <prettyprint.hpp>

class Foo
{
    int arr[10];

    template <typename T, T> struct Filler { };
    template <typename T, bool, unsigned int, T...> struct FillHelper { };

    template <typename T, unsigned int I, T V, T ...Vals>
    Foo(FillHelper<T, true, I, V, Vals...>)
    : arr { V, Vals... } { }

    template <typename T, unsigned int I, T V, T ...Vals>
    Foo(FillHelper<T, false, I, V, Vals...>)
    : Foo(FillHelper<T, I == 1, I - 1, V, V, Vals...>()) { }

    template <typename T, unsigned int N, T V>
    Foo(Filler<T, V>, T const (&)[N])
    : Foo(FillHelper<T, N == 1, N - 1, V>())
    { }

public:
    Foo() : Foo(Filler<int, 2>(), arr)
    {
        std::cout << "Foo: " << arr << "\n";
    }
};


int main()
{
    Foo x;
}

Output: Foo: [2, 2, 2, 2, 2, 2, 2, 2, 2, 2] 输出: Foo: [2, 2, 2, 2, 2, 2, 2, 2, 2, 2]

to set all the values in an array to a default value x, you can use something like std::fill_n 要将数组中的所有值设置为默认值x,您可以使用类似std :: fill_n的内容

for example to set all the elements to -1 例如,将所有元素设置为-1
std::fill_n(array, 100, -1); std :: fill_n(array,100,-1);

set each of the values in the array to the same character 将数组中的每个值设置为相同的字符

Why not using memset ? 为什么不使用memset? For example init each of the values to 0x01 例如,将每个值初始化为0x01

board::board()
{
    memset(spot, 0x01, sizeof(spot));
}

您只能使用for循环分配每个元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM