开发者

Problems with map as array

Good day!

I'm making nondeterministic finite automata with c++. I want to make a transition table. As you know, it should return a set of states. For example, table[state][symbol] should return {q0,q1, etc..}.

I'm using std::map and std::set for this. I found this useful example : how to use stl::map as two dimension array

So, i wrote some code:

std::map <set<state>, std::map<state,char> > transitionTable;

But when i'm trying to acsess the table like

set<state> result = transitionTable[oldState][symbol];

I get an error:

C:\NFA2\main.cpp||In function 'std::set<state, std::less<state>, std::allocator<state> > delta(state, char)':|
C:\NFA2开发者_JAVA百科\main.cpp|17|error: no match for 'operator[]' in 'transitionTable[oldState]'|

Can you help me?

Thanks in advance.


From your description it sounds like you want (typedefs for readability):

typedef std::set<state> StateSet;
typedef std::map<char, StateSet> SymbolToStatesMap;
typedef std::map<state, SymbolToStatesMap> TransitionTable;

TransitionTable transitionTable;

Keep in mind that the first type argument to map is the key type, so if you want an access like this:

A a;
B b;
C c = myMap[a][b];

the map type should be:

std::map<A, std::map<B, C> >

To actually be able to use custom types as a key in a map, they have to implement operator<:

class state {
    // ...
public:
    bool operator<(const state& rhs) const {
        // ... custom stuff here
    }

Alternatively you can pass a custom comparer to map.


I assume that oldState is a set. If that is the case, transitionTable[oldState] returns you a map and transitionTable[oldState][symbol] is invalid if symbol is not a set and hence error.


std::map <set<state>, std::map<state,char> > transitionTable;

You have a set as key in the map, not a state.


What are the types of oldState and symbol?

From what I understand of your problem, I suspect that the type of transitionTable should be:

std::map<std::set<state>, std::map<char,std::set<state> > transitionTable;

At least, this would give the mapping: (set_of_states, char) -> set_of_states, if used as in your example, where oldState is a set of states, and symbol is a char.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜