当你有一长串要检查的数组列表时该怎么办? [英] What to do when you have a long list of arrays to check against?
问题描述
我得到了以下代码,其中我指定了很多字符数组,然后将它与通过mbed设备上的串行连接接收的字符进行比较。
HexSearch。 cpp:
I got the following code, where I specify a lot of char arrays and then compare it to one received through serial connection on an mbed device.
HexSearch.cpp:
#include "HexSearch.h"
void searchFunction(int num, char msg[]) {
static const char readReq[] = { 0x92 };
static const char readResp[] = { 0x00, 0x02, 0x12, 0x34, 0xA1 };
static const char writeReq[] = { 0x0A, 0xE0 };
static const char writeResp[] = { 0x00, 0x02, 0x11, 0x01, 0x98 };
static const char resetReq[] = { 0x00, 0xFF };
static const char resetResp[] = { 0x00, 0x21, 0x23, 0x0E, 0xAE, 0x11, 0x3A };
static const char verReq[] = {0x00, 0xA2};
static const char verResp[] = {0x00, 0x03, 0x82, 0xAA, 0x07, 0x88, 0xA9};
static const char typeReq[] = {0x00, 0x67};
static const char typeResp[] = {0x00, 0x03, 0x00, 0x00, 0xC4, 0x77};
static const char askReq[] = {0x00, 0x55};
static const char askResp[] = {0x00, 0x01, 0xFE, 0xFF};
if (num == 4) {
replyMsg(msg, 2, 3, readReq, readResp, sizeof(readResp) / sizeof(readResp[0]));
}
else if (num == 5) {
replyMsg(msg, 2, 4, writeReq, writeResp, sizeof(writeResp) / sizeof(writeResp[0]));
replyMsg(msg, 2, 4, resetReq, resetResp, sizeof(resetResp) / sizeof(resetResp[0]));
replyMsg(msg, 2, 4, verReq, verResp, sizeof(verResp) / sizeof(verResp[0]));
replyMsg(msg, 2, 4, typeReq, typeResp, sizeof(typeResp) / sizeof(typeResp[0]));
replyMsg(msg, 2, 4, askReq, askResp, sizeof(askResp) / sizeof(askResp[0]));
}
}
void replyMsg(char msg[], int startArr, int endArr, const char* receiv, const char* resps, int respL) {
if (std::equal(msg + startArr, msg + endArr, receiv)) {
for (int x = 0; x < respL; x++) {
serialPC.putc(resps[x]);
}
}
}
我的问题是我不觉得这是初始化字符数组的好方法。我认为有可能将它们声明为一个单独的文件,然后在这个函数或类似的东西中使用?我想我觉得在一个函数开始时将它们全部初始化是非常混乱的。
我尝试了什么:
代码可以运行并完成任务,我只是想知道是否有更好的方法来执行此操作。
My problem is that I don't feel like this is a good way to initialize the arrays of chars. I would of thought it is possible to maybe declare them as a separate file to then use in this function or something similar? I guess I feel like it is quite messy to initialise them all at the start of a function.
What I have tried:
The code works and does its task well, I just want to know if there is a better way to do this.
推荐答案
不完全是你要求的,但我想你会明白这个想法
Not exactly what you've asked for, but I suppose you'll get the idea
#include <cstdint>
#include <iostream>
#include <vector>
#include <map>
using bvect = std::vector<uint8_t>; // 'vector of bytes'
class Replier
{
static std::map< bvect, bvect > m_reply_map;
public:
static void reply( const bvect & msg)
{
auto it = m_reply_map.find( msg );
std::cout << "message: < ";
for ( auto b : msg)
std::cout << std::hex << static_cast<unsigned>(b) << " ";
if ( it != m_reply_map.end())
{
std::cout << ">, reply: < ";
for ( auto b : it->second)
std::cout << std::hex << static_cast<unsigned>(b) << " ";
std::cout << ">\n";
}
else
std::cout << ">, no reply (unrecognized message)\n";
}
};
std::map< bvect, bvect > Replier::m_reply_map =
{
{ { 0x92 }, { 0x00, 0x02, 0x12, 0x34, 0xA1 } },
{ { 0x0A, 0xE0 }, { 0x00, 0x02, 0x11, 0x01, 0x98 } },
{ { 0x00, 0xFF }, { 0x00, 0x21, 0x23, 0x0E, 0xAE, 0x11, 0x3A } },
{ { 0x00, 0xA2 }, { 0x00, 0x03, 0x82, 0xAA, 0x07, 0x88, 0xA9 } },
{ { 0x00, 0x67 }, { 0x00, 0x03, 0x00, 0x00, 0xC4, 0x77 } },
{ { 0x00, 0x55 }, { 0x00, 0x01, 0xFE, 0xFF } }
};
int main()
{
Replier::reply( { 0x0A, 0xE0 } );
Replier::reply( { 0x92 } );
Replier::reply( { 0x00, 0xE0 } );
Replier::reply( { 0x00, 0x67 } );
}
您的设计没有任何问题。
您可以放置所有在他们自己的静态类中的常量,以更好地隔离。
越来越多的程序正在沙盒;即在没有用户接受的情况下很少或没有文件访问。
因此,如果你可以嵌入所需资源,那么无论如何都会占用空间,那么你领先于它。
在代码中构建表有时比查看抽象数据然后处理它更容易。
Nothing wrong with your design.
You could put all your constants in their own static class for better "isolation".
More and more programs are getting "sand boxed"; i.e. little or no file access without user acceptance.
So, if you can "embed" a needed resource, that would take up room as a file anyway, then you are ahead of it.
And building tables in code is sometimes easier than looking at abstract data and then dealing with that.
它根本不是太糟糕,但是当C ++你可以使用一些类设计,但也使用静态变量。
提示:sizeof运算符由编译器解析,所以你不会因此而受到性能损失。
你可以优化它,因为起始偏移量是恒定的
It isnt too bad at all, but when C++ you can use some class design, but also use static variables.
Hint: the sizeof operator gets resolved by the compiler, so you get no performance penalty for it.
You can optimize that, because the start offset is constant
replyMsg(msg, 2, ...
设计提示:该函数没有错误处理。
design tip: in that function is no error handling.
这篇关于当你有一长串要检查的数组列表时该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!