在c ++中将对插入到map中 [英] Insert pair into map in c++
本文介绍了在c ++中将对插入到map中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下地图结构:
std::map<std::pair<int, char>, int> transitions;
表示非确定性自动机的转换,每个自动机分为2个整数和一个字符I,J和C,代表转换状态,即转换状态从状态i到状态J,并带有字符C.
That represent the transition of the nondeterministic automaton, each with 2 integers and a character, I, J and C, representing the states where the transition, ie, the transition goes from state i to state J with the character C.
0 0 a
0 1 a
1 1 b
1 2 c
1 3 c
3 4 d
4 4 d
4 5 d
我想插入它。我如何做?
I wish to insert into it. How I can do?
我想到了以下几点:
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 ));
}
我不认为是这样做的方式,我是新手使用地图库。
I do not think that is the way to do it, I am newbie using the map library.
推荐答案
我唯一看到错误的是你需要一个对象:
The only thing I see wrong is you need an object:
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而不是使用:
You could even take it a step further and use key_type rather than 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 ));
}
这篇关于在c ++中将对插入到map中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文