简体   繁体   English

将值插入多图向量

[英]Inserting values into a vector of multimap

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. 代表自动机的过渡,我使用std :: multimap的向量进行过渡。 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? 有什么建议么?

You never use stateOrigin, so I'm pretty sure it's wrong (unless I've completely misunderstood your intent). 您永远不会使用stateOrigin,所以我很确定这是错误的(除非我完全误解了您的意图)。 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. 还要注意,在C ++ 11中,除非您需要有效访问给定状态的所有触发器,否则可能要改用std::unordered_map

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM