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,
4 个解决方案
#1
4
You cannot do this in C++03. However, it is possible to initialize arrays in a constructor initializer list in C++11:
你不能在C ++ 03中这样做。但是,可以在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). Also, any missing elements are zero-initialized (e.g. char spot[4] { 1, 2 };
).
您可以输入值,或者提出一个魔术可变参数模板来提供“N个重复值”(只要类型是整数)。此外,任何缺失的元素都是零初始化的(例如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]
#2
0
to set all the values in an array to a default value x, you can use something like std::fill_n
for example to set all the elements to -1
std::fill_n(array, 100, -1);
要将数组中的所有值设置为默认值x,您可以使用类似std :: fill_n的方法将所有元素设置为-1 std :: fill_n(array,100,-1);
#3
0
set each of the values in the array to the same character
将数组中的每个值设置为相同的字符
Why not using memset ? For example init each of the values to 0x01
为什么不使用memset?例如,将每个值初始化为0x01
board::board()
{
memset(spot, 0x01, sizeof(spot));
}
#4
0
You can only assign each element using for loop.
您只能使用for循环分配每个元素。
#1
4
You cannot do this in C++03. However, it is possible to initialize arrays in a constructor initializer list in C++11:
你不能在C ++ 03中这样做。但是,可以在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). Also, any missing elements are zero-initialized (e.g. char spot[4] { 1, 2 };
).
您可以输入值,或者提出一个魔术可变参数模板来提供“N个重复值”(只要类型是整数)。此外,任何缺失的元素都是零初始化的(例如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]
#2
0
to set all the values in an array to a default value x, you can use something like std::fill_n
for example to set all the elements to -1
std::fill_n(array, 100, -1);
要将数组中的所有值设置为默认值x,您可以使用类似std :: fill_n的方法将所有元素设置为-1 std :: fill_n(array,100,-1);
#3
0
set each of the values in the array to the same character
将数组中的每个值设置为相同的字符
Why not using memset ? For example init each of the values to 0x01
为什么不使用memset?例如,将每个值初始化为0x01
board::board()
{
memset(spot, 0x01, sizeof(spot));
}
#4
0
You can only assign each element using for loop.
您只能使用for循环分配每个元素。