c++ - 在用户定义类的无序集中使用散列函数
问题描述
我希望你们一切都好。
我目前正在研究 C++ 中的 DFA 实现(希望使用它以便稍后编写词法分析器)。我在这方面仍然很糟糕,如果我的问题听起来很愚蠢,请原谅我。
我有两个主要课程。一个 FSM(有限状态机)类,自然包含状态、转换和字母表。我选择用 unordered_set 类 State 来表示我的状态集的转换,以避免以后重复。我知道我必须编写自己的散列函数和我自己的相等比较器函数,我试图在下面的代码中做到这一点。但是,我有无穷无尽的不可修复的错误,这些错误似乎与我刚刚实现的这两件事有关。
FSM.h
#ifndef FSM_H
#define FSM_H
#include <vector>
#include <string>
#include "state.h"
#include <unordered_set>
#include <map>
#include <tuple>
#include <iostream>
#include <iterator>
#include <functional>
#include <algorithm>
namespace std
{
template<>
class hash<State>
{
public:
size_t operator() (const State &state) const
{
return std::hash<std::string>()(state.name);
}
};
}
struct isEqual
{
bool operator() (const State& lhs, const State& rhs)
{
return (lhs.name == rhs.name);
}
};
class FSM
{
FSM() = default;
FSM(State,
std::unordered_set<State>,
std::unordered_set<State>,std::unordered_set<std::tuple<State, std::string, State>>,
std::unordered_set<std::string>);
~FSM();
State initialState;
std::unordered_set<std::string> alphabet;
std::unordered_set<State> states;
State init;
std::unordered_set<State> finalStates;
std::unordered_set<std::tuple<State,std::string,State>> transitions; //(CurrentState + input -> Next State)
State currentState;
};
#endif
如您所见,我对命名空间 std 中的散列使用了过度专业化。至于State Class,目前如下:
#ifndef STATE_H
#define STATE_H
#include <string>
class State
{
public:
State(std::string _name) {name = _name;}
~State();
std::string name;
bool operator ==(const State& rhs) const
{
return (name == rhs.name);
}
};
#endif
FSM.cpp
#include "fsm.h"
FSM::FSM(State _initialState,
std::unordered_set<State> _states,
std::unordered_set<State> _finalStates,
std::unordered_set<std::tuple<State,std::string,State>> _transitions,
std::unordered_set<std::string> _alphabet )
{
initialState = _initialState;
states = _states;
finalStates = _finalStates;
transitions = _transitions;
alphabet = _alphabet;
}
编译命令如下:
c++ -std=c++11 -o testing *.cpp
如何修复我的散列函数和/或比较相等函数以使其正常工作?
任何帮助将不胜感激!谢谢 !!!
解决方案
问题如下:
FSM(State,
std::unordered_set<State>,
std::unordered_set<State>,std::unordered_set<std::tuple<State, std::string, State>>,
std::unordered_set<std::string>);
~FSM();
void addTransition(std::tuple<State,std::string,State >);
void deleteTransition(std::tuple<State,std::string,State>);
void addState(State _state);
void deleteState(State);
void newAlphabet();
void addTransition();
void input(std::string input);
void setCurrentState(State);
void setInitialState (State);
void setFinalStates(std::unordered_set<State> );
void setTransitions(std::unordered_set<std::tuple<State,std::string,State>>);
void setStates(std::unordered_set<State> );
State initialState;
std::unordered_set<std::string> alphabet;
std::unordered_set<State> states;
State init;
std::unordered_set<State> finalStates;
std::unordered_set<std::tuple<State,std::string,State>> transitions; //(CurrentState + input -> Next State)
State currentState;
您声明了很多函数,但从未定义它们,因此编译器不会重新识别它们......
推荐阅读
- c# - 保存“结果文件夹”的日期问题
- node.js - beforeUpdate 续集
- python - 如何停止运行不和谐的机器人进程(python)
- java - 如何将此 JSON 转换为 JAVA android 中的对象?
- c++ - 为什么这会导致Seg。故障,如何使用 GDB 调试它?
- c - 无法对动态分配节点的链表进行排序
- python - 在第一个空格之后排序列表项,然后是相同的单词
- node.js - 删除类的对象实例-NodeJS
- azure-logic-apps - Azure 事件中心日志使用查询对流进行排序
- c++ - c++中的`function() const &`中的`&`是什么?