C++ 自定义表ADT(map)FSM(有限状态机)
我必须编写模拟std::map的代码。我有一个模板化的pair类和一个table类 我得到了一张过渡表:C++ 自定义表ADT(map)FSM(有限状态机),c++,arrays,dictionary,data-structures,C++,Arrays,Dictionary,Data Structures,我必须编写模拟std::map的代码。我有一个模板化的pair类和一个table类 我得到了一张过渡表: A B C D E nke fa1 ok1 fa1 fa1 fa1 ok1 ok2 fa2 fa2 fa2 fa2 ok2 ok3 fa3 fa3 fa3 fa3 ok3 nke nke
A B C D E
nke fa1 ok1 fa1 fa1 fa1
ok1 ok2 fa2 fa2 fa2 fa2
ok2 ok3 fa3 fa3 fa3 fa3
ok3 nke nke nke nke nke
fa1 fa2 fa2 fa2 fa2 fa2
fa2 fa3 fa3 fa3 fa3 fa3
fa3 nke nke nke nke nke
A B C D E
nke
ok1
ok2
ok3 alarm alarm alarm unlock alarm
fa1
fa2
fa3 alarm alarm alarm alarm alarm
和行动表:
A B C D E
nke fa1 ok1 fa1 fa1 fa1
ok1 ok2 fa2 fa2 fa2 fa2
ok2 ok3 fa3 fa3 fa3 fa3
ok3 nke nke nke nke nke
fa1 fa2 fa2 fa2 fa2 fa2
fa2 fa3 fa3 fa3 fa3 fa3
fa3 nke nke nke nke nke
A B C D E
nke
ok1
ok2
ok3 alarm alarm alarm unlock alarm
fa1
fa2
fa3 alarm alarm alarm alarm alarm
基本上,我假设对一个安全系统进行编码,在有人按组合键BAAD后,门就会打开。其他任何东西都会发出警报
我理解这个概念,但不理解如何创建表的映射
在下面的头文件中
int (*Mapping)( Key k);
我不知道如何使用action和transition表将键映射到值
如何按主键和值映射表的值
请询问我是否需要提供更多信息
表头文件:
#ifndef TABLE_H
#define TABLE_H
#include <iostream>
#include <stdexcept>
#include <string>
#include "pair.h" // Pair class
using namespace std;
// implements a table containing key/value pairs.
// a table does not contain multiple copies of the same item.
// types T and Key must have a default constructor
template < class Key, typename T >
class Table
{
public:
Table(); //default constructor
~Table(); //destructor
typedef Key key_type;
// for convenience
private:
// table implemented using a one dimensional array of key-value pairs
int tableSize;
Pair< key_type, T > *the_table;
int (*Mapping)( Key k);
// Mapping is a function which maps keys to
// an array index; ie a key to address mapping
// the idea is that Mapping will act on a given key
// in such a way as to return the relative postion
// in the sequence at which we expect to find the key
// Mapping will be used in the remove, add, lookup. =
// member functions and copy constructor
public:
// for debugging
void print();
Table( int n, int (*map)( Key k) );
// map is a function to map key to address
// in the implementation
// set the function ie have the code line
// Mapping = map;
// populates table with default values
// for the class Key and T
bool insert( Pair< Key, T > kvpair );
// return true if item could be added to the
// table false if item was not added.
bool remove( const Key aKey );
// erase the key/value pair with the specified key
// from the table and return if successful
// removed item is replaced with default
// values for Key and T
T lookUp (const Key aKey) ;
// what if key not in table??
//need copy constructor
//need destructor
// void operator= ( const Table & initTable );
// bool empty() const;
// is the table empty?
// bool full() const;
// is the table full?
// int size() const;
// return the number of elements in the table
// bool isIn(const Key& key) const;
// returns true/false in response to obvious question
};
#include "table.t"
#endif
你有35对可能的。您是否需要一个哈希函数,将每对映射到一个唯一的数组索引?另外,使用模板的意义是什么?有什么理由不使用两个枚举,比如名称空间报警{enum state{nke,//…};枚举输入{A,//…}?是的,我想问的是如何将每对映射到唯一的数组索引。使用模板的目的是使代码具有通用性。让我试着改写一下。使用模板时,模板参数通常有某种用途。Key和T应该代表什么?密钥应该是一个状态nk0、…、一个转换a、B、…、一个动作报警、…,还是它们的一对/组合?您在中声明不能有重复的项目。这是否意味着没有像普通映射那样的重复键或没有重复的键值对?key:nke,bt:ok1 key表示一对,T表示值。