我必须基本上编码模仿std :: map的代码。我有一个模板化的对类和一个表类。
我获得了转换表:
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);
我无法弄清楚如何使用操作和转换表将键映射到值。
如何按主键和值映射表的值?
请询问我是否需要提供更多信息,
表头文件:
#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