Moh*_*oun 9 c++ redundancy c++11 c++14
在编程时,我遇到了一些带有代码的墙.它看起来像这样:

这就是问题所在.我拍了一张漂亮的截图来减轻我的内疚.漂亮的颜色无法弥补缺乏可维护性.我几乎不知道如何概括这样的代码.
好吧,考虑第3和第6个参数的周期性.它也与其他参数的周期性一致.如果我们使用数组,这样的东西将允许我们将此代码转换为具有9行的循环.这是一个改善,因为我们下降了66%.但是,这还不够好.如果将其更改为1行,那将是最好的.这将至少使它有点更易于维护.
好吧,让我们这样说吧.那里的代码可能也是错的.
seh*_*ehe 16
嗯,花了一些时间来分析模式.
当然,首先我使用http://www.onlineocr.net/从屏幕截图中获取文本.然后我开始匹配突出显示模式.
make_cube实际上有两个(x,y,z)元组z值结束(y,z)元组结尾的子组组成这使得它成为生成循环的"明显"材料.经过大约20分钟的重构后,我感到很沮丧
for (auto&& zs : { tie(rmin_z, imin_z), tie(imin_z, imax_z), tie(imax_z, rmax_z) })
for (auto&& ys : { tie(rmin_y, imin_y), tie(imin_y, imax_y), tie(imax_y, rmax_y) })
for (auto&& xs : { tie(rmin_x, imin_x), tie(imin_x, imax_x), tie(imax_x, rmax_x) })
{
*out++ = make_cube(get<0>(xs), get<0>(ys), get<0>(zs), get<1>(xs), get<1>(ys), get<1>(zs));
}
Run Code Online (Sandbox Code Playgroud)
但是你会注意到循环范围的规律性.实际上我们有一个类似的序列
coord const sequence[] = { rmin, imin, imax, rmax };
Run Code Online (Sandbox Code Playgroud)
我们选择连续的对: (rmin, imin), (imin, imax), (imax, rmax)
// we take all consecutive pairs (warning: ignoring the `(rmax, rmin)` closing pair here)
vector<pair<coord, coord>> pairs;
transform(begin(sequence), prev(end(sequence)), back_inserter(pairs), [](coord const& it) { return std::make_pair(*(&it+0), *(&it+1)); });
Run Code Online (Sandbox Code Playgroud)
现在我们可以更直接地循环它.我还发明了一种简单的Cube类型,它允许我们打印生成器循环的结果,以便您可以在DEBUG模式下验证结果:
for (auto zs : pairs) for (auto ys : pairs) for (auto xs : pairs)
*out++ = Cube { { xs.first.x, ys.first.y, zs.first.z }, { xs.second.x, ys.second.y, zs.second.z } };
Run Code Online (Sandbox Code Playgroud)
#include <iostream>
#include <algorithm>
#include <vector>
#include <array>
int main() {
#ifdef NDEBUG
typedef double T;
struct coord { T x,y,z; };
coord rmin { 0, 1, 2 },
imin { 3, 4, 5 },
imax { 6, 7, 8 },
rmax { 9, 10, 11 };
#else
typedef const char* T;
struct coord { T x,y,z; };
coord rmin { "rmin_x", "rmin_y", "rmin_z" },
imin { "imin_x", "imin_y", "imin_z" },
imax { "imax_x", "imax_y", "imax_z" },
rmax { "rmax_x", "rmax_y", "rmax_z" };
#endif
using namespace std;
// the source sequence
coord const sequence[] = { rmin, imin, imax, rmax };
// we take all consecutive pairs (warning: ignoring the `(rmax, rmin)` closing pair here)
vector<pair<coord, coord>> pairs;
transform(begin(sequence), prev(end(sequence)), back_inserter(pairs), [](coord const& it) { return std::make_pair(*(&it+0), *(&it+1)); });
// Now we build cubes. The `make_cube` interface implied it requires two
// coordinates to be constructed:
struct Cube { coord p1, p2; };
std::array<Cube, 3*3*3> cubes;
// generate!
auto out = cubes.begin();
for (auto zs : pairs) for (auto ys : pairs) for (auto xs : pairs)
*out++ = Cube { { xs.first.x, ys.first.y, zs.first.z }, { xs.second.x, ys.second.y, zs.second.z } };
// debug print
for(auto const& c : cubes)
std::cout << "make_cube(" << c.p1.x << ", " << c.p1.y << ", " << c.p1.z << ", " << c.p2.x << ", " << c.p2.y << ", " << c.p2.z << ")\n";
}
Run Code Online (Sandbox Code Playgroud)
关于这个问题
这真的是个问题吗?
好吧,让我们这样说吧.那里的代码可能也是错的
事实上,我有点怀疑你是否涵盖了所有案件.看到第一条评论:
// we take all consecutive pairs (warning: ignoring the `(rmax, rmin)` closing pair here)
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
367 次 |
| 最近记录: |