- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一张用 C++ 编写的 2D map ,代表一个游戏场。在每个字段上都可以有一种交通工具(例如汽车、飞机......(所有这些都是以前在枚举中定义的))。
bool MainClass::isTrue(int x, int y, int w, int h)
{
for (int m = y; m < h; m++)
{
for (int n = x; n < w; n++)
{
if (map_.at(n-1).at(m-1) == SubClass::PartOfEnum::CAR ||
map_.at(n-1).at(m-1) == SubClass::PartofEnum::BOAT ||
map_.at(n-1).at(m-1) == SubClass::PartofEnum::SHIP ||
map_.at(n-1).at(m-1) == SubClass::PartofEnum::PLANE)
return (0);
}
}
return (1);
// other code ..
}
正如某些人所看到的,我正在遍历这张 map 的各个部分,想知道在某些定义的坐标上是否存在其中一种交通工具。问题是 for 循环内的代码对枚举的每个部分重复。所以我的问题是,是否有解决此类问题的方法,只需编写一行代码而不是四行代码。也许将枚举的各个部分保存在一个单独的变量中,然后将这个变量用于循环。 original-enum 还包含一些其他不应属于此循环的对象(在 original-enum 中还有一些其他交通工具,如自行车,但这里只有这四种相关)。提前致谢
最佳答案
So my question is, if there is a solution for this kind of problem to write just one line of code instead of four
您需要四次比较,但您不需要多次访问 map_
。一个方便的实用程序应该做的:
bool isVehicle(SubClass::PartOfEnum test) {
return test == SubClass::PartOfEnum::CAR ||
test == SubClass::PartofEnum::BOAT ||
test == SubClass::PartofEnum::SHIP ||
test == SubClass::PartofEnum::PLANE;
}
然后你要测试的是:
for (int n = x; n < w; n++)
{
if (isVehicle(map_.at(n-1).at(m-1)))
return (0);
}
相当直接的重构。除了单一访问之外的其他好处是,人们可以知道条件正在检查什么,而无需深入研究如何检查它的细节。如果需要,现在您可以相当轻松地在多个地方使用相同的条件。更不用说对该函数的任何更新都将应用于您使用它执行检查的所有地方。
关于for循环中的c++冗余代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50085831/
我有下面提供的“示例代码”,我觉得这很多余,并且想请您与我一起解释是否有任何更少冗余的方法来处理以下代码。 public interface Bars { FOO1 FOO1 = (FOO1)
我们刚刚测试了一个由 2 个服务器组成的 AppFabric 集群,我们在其中删除了“领导”服务器。第二个服务器对它的任何请求超时并出现错误: Microsoft.ApplicationServer.
我正在设计一个关系数据库 - 底层是 MySQL - DBDesigner 4 . 我有 3 个表:module、page 和 lang。每个模块属于一个页面,每个页面都有特定的语言: 外键 pag
我的 Fraction 程序运行流畅,但 NetBeans IDE 告诉我以下 if 是多余的: public boolean equals(Object other) { Fraction bo
下面的代码显然是多余的,但根据我的经验,我经常使用这种模式。有没有更好的方法在 python 中执行此操作? if re.search("at (\d{1,2}):\d{2}", p): a=
我有一个用具体示例说明的一般性问题。当所有组件对象都已测试时,您建议对复合对象进行多少测试? 作为具体示例,请考虑下面的 NullTerminatedStringReader。它从字节缓冲区中读取一个
if ( a > b) { return true; } return false; 对于上面的代码,Netbeans 给出了 “Redundant if statement” 警告并建议将其更改
当我添加一些约束时,例如: create table Test( IDTest int primary key, Credit int not null constraint Credit
此代码适用于 Microchip 的 PIC32MX 微处理器。他们的编译器本质上是 GCC 3.4。 我倾向于使用 GCC 的 __packed__ attribute将位域打包到一个 union
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我正在为我的应用程序使用apns通知,为此,我将apns设备 token 存储在我的sql数据库中。 问题在于,每次用户删除或安装该应用程序时,它都会生成一个新的设备 token ,并将其存储在数据库
Action Keyname Type Unique Packed Column Cardinality Collation Null Comment Edit Dro
我正在开发一个使用 ARM Cortex-M0 处理器的项目。在这个项目中,我需要提供计时器支持(CMSDK (SSE-200)计时器)。 因此,在 vector 表中,在 TIMER0_IRQn 表
有没有写的理由 corsFilter.setAllowedOrigins(new HashSet(Arrays.asList("*"))); 其中allowedOrigins在ReSTLet框架中的定
我正在创建一个包含 4 个链接的 HTML/CSS 页面, Home.html Details.html ContactMe.html AboutUs.html 我想在所有关联的 HTML 页面中将其
我试图理解并使用其他人编写的代码,但由于我对 typedef 经验不多。 , 我有时会感到困惑。 有两个不同的头文件,一个继承另一个,并且在两个文件上声明相同的typedef。 为什么会出现冗余,如何
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我有一个用于改造的 POJO 类。 public class AppData(){ String a; String b; String c;
我想知道: 编写这段代码: DataRow[] g = new DataRow[1] ; var t=new StringBuilder().AppendFormat("{0}", g[0]["a
只是一个性能问题...... 假设我有 5 个类,每个类都引用了 System.Data 和一个自己开发的库。这 5 个类是类库的一部分,最终将被构建并发布到一些 Web 应用程序作为引用。 通过将引
我是一名优秀的程序员,十分优秀!