我通常使用枚举来保持两个数组的一致性,方法如下:
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<div class="main-container">
<div class="select-container" data-bind="click: toggleOptions">
<select data-bind="options: [selectedItemsStr]"></select>
</div>
<div class="options-container" data-bind="css: { 'shown': optionsShown }">
<div class="options" data-bind="foreach: items">
<label data-bind="css: { 'checked': isChecked }">
<input type="checkbox" data-bind="checked: isChecked">
<span data-bind="text: label"></span>
</label>
</div>
<div class="button-container">
<button type="button" data-bind="click: confirmSelection">OK</button>
<button type="button" data-bind="click: closeOptions">Cancel</button>
</div>
</div>
</div>
此代码对enum foo {
ZERO = 0,
ONE,
TWO,
};
int int_array[] = {
[ZERO] = 0,
[ONE] = 1,
[TWO] = 2
};
char *str_array[] = {
[ZERO] = "ZERO",
[ONE] = "ONE",
[TWO] = "TWO"
};
编译正常,但在c
模块中使用时会抛出错误。
cpp
错误是两个数组声明中的每一行。这里的问题是什么?
答案 0 :(得分:1)
它不是C ++的有效语法。您可以通过以下方式初始化数组:
String hex = "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000009796f6f6f6f6f6f6f6f0000000000000000000000000000000000000000000000";
StringBuilder output = new StringBuilder();
for (int i = 0; i < hex.length(); i+=2) {
String str = hex.substring(i, i+2);
output.append((char)Integer.parseInt(str, 16));
}
System.out.println(output.toString().trim());
答案 1 :(得分:1)
C ++不支持所谓的指示符。 C中允许的初始化。
因此编译器会发出消息。
在C ++中,你必须按以下方式编写
int int_array[] = { 0, 1, 2 };
const char *str_array[] = { "ZERO", "ONE", "TWO" };
^^^^^^