如何处理以 2D 数组编写的条件
How to handle conditions written in 2D array
考虑这个2D数组:
A B C D E
A 1 0 0 0 0
B 0 1 1 0 0
C 0 1 1 0 0
D 0 0 0 1 1
E 0 0 1 1 1
这意味着 A可以映射到 A,但 sureshot 不能映射到 B、C、D 或 E。
类似地,B可以映射到 B 和 C,但不能映射为 A、D 和 E。
此处可能意味着它可能被映射或不映射。
现在,我必须编写一个 c++ 代码来确保此映射成立,如果它不成立,则断言。
我已经像这样写了一个关于它的 if else 代码(骨架(。我正在检查 B 应该不确定射击匹配的条件。
if (checking_for_B) {
if (B is mapped with A || B is mapped with D || B is mapped with E) {
assert();
}
}
同样,我还必须为 A、B、C、D 和 E 编写 if 条件。我对这种方法不满意。如果你是我,你会写什么?
我会尝试通过将字母转换为行和列索引来解决您的问题,并检查是否允许该组合。
那么你不需要"checking_for_A"后跟"checking_for_B"等。只是像
if (my_map[row_letter_index][column_letter_index])
{
// All okay, allow
}
else
{
// Not allowed, report or abort or throw exception or anything else
}
相关文章:
- MongoDB 使用数组中的 OR 条件构建查询
- C++ 将二维数组索引与条件语句中的函数值进行比较
- 将子字符串与字符串数组进行比较的 IF 条件会在C++中产生错误
- 如何在Char数组中搜索Char在IF条件语句中
- 整个数组作为C 的条件
- 有没有办法创建一个花哨的迭代器和相应的新数组,以便检查每个索引的索引值的条件?
- 使用位集获取数组项的条件总和,而无需使用循环
- 将非类非数组 prvalue 转换为符合 cv 条件的类型是否合法?
- 如何在没有 c++ 中的数组/算法的情况下对两个条件的字符串进行排序
- Bool Vector 2D数组在IF条件下使用
- 有条件的断点修饰符以查看数组是否已更改
- 有条件地确定字符串是否是数组的一部分
- 使用两个条件对结构数组进行排序 C++
- 无法弄清楚为什么我的程序在条件不为真时执行 if 语句(数组)
- 动态数组(如果满足条件供以后使用)
- 二维数组、IF条件检查
- 是否可以使用有条件选择的字符串文本初始化字符数组
- 条件运算符和数组/返回指向数组的指针
- 使用数组作为惠勒循环条件
- 有条件的大平面数组遍历和令人惊讶的短循环执行时间