0

I have this structure based multimap and a vector for this structure:

typedef std::multimap<char, int> tr;
vector <tr> transitions;

I want to fill the array with values ​​like:

0 0 a
0 1 a
1 1 b
1 2 c
1 3 c

which represent the transitions of an automaton, and i use a vector of std::multimap for the transitions. This assumes that each state corresponds to an integer. How I could do this?. I try:

for (j=0; j<numberTransitions;j++){
    cin>> stateOrigin>>stateDestination>>transitionCharacter;
    transitionsStates.insert(pair<char, int>(transitionCharacter, stateDestination));
    transitions.push_back (transitionsStates);
}

But I'm not sure if it's correct. Any suggestions?

Cœur
  • 37,241
  • 25
  • 195
  • 267
franvergara66
  • 10,524
  • 20
  • 59
  • 101

1 Answers1

1

You never use stateOrigin, so I'm pretty sure it's wrong (unless I've completely misunderstood your intent). I think what you want is more like this:

typedef std::pair<int, char> trigger;
std::map<trigger, int> transitions;
⋮
transitions.insert(make_pair(make_pair(orig, chr), dest));

To drive the state machine, you'd use something like this:

auto newState = transitions.find(make_pair(oldState, inputChar));
if (newState != transitions.end()) {
    state = newState;
}

Also note that with C++11 you probably want to use std::unordered_map instead, unless you need efficient access to all the triggers for a given state.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365