我有以下地图结构:
std::map<std::pair<int, char>, int> transitions;
表示非确定性自动机的转换,每个自动机具有2个整数和一个字符I,J和C,表示转换的状态,即转换从状态i进入状态J并带有字符C. / p>
0 0 a
0 1 a
1 1 b
1 2 c
1 3 c
3 4 d
4 4 d
4 5 d
我希望插入其中。我该怎么做?
我考虑过做以下事情:
typedef map<pair<int, char>, int> transitions;
for (j=0; j<numberTransitions;j++)
{
cin>> stateOrigin>>stateDestination>>transitionCharacter;
transitions.insert(transitions::value_type(std::make_pair(stateOrigin,transitionCharacter), stateDestination ));
}
我认为不是这样做的,我是新手使用地图库。
答案 0 :(得分:4)
我唯一看错的是你需要一个物体:
transitions.insert(transitions::value_type(std::make_pair(stateOrigin,transitionCharacter), stateDestination ));
^^^^^^^^^^^ is a type
/// Should be
transitions trans;
// Then later in the loop.
trans.insert(transitions::value_type(std::make_pair(stateOrigin,transitionCharacter), stateDestination ));
你甚至可以更进一步,使用key_type而不是make pair:
trans.insert(transitions::value_type(transitions::key_type(stateOrigin,transitionCharacter), stateDestination ));
// or
for(/*STUFF*/)
{
typedef transitions::value_type value_type;
typedef transitions::key_type key_type;
trans.insert(value_type(key_type(stateOrigin,transitionCharacter), stateDestination ));
}