我需要将 3 个值插入 std::map<int,std::pair<float,float> >
类型的映射中.
这样 map 的数据就会是{22 32626.23 53232.63
std::map<int,std::pair<float,float> > my_MainMap;
std::map<float,float> myMap1;
int iValue;
float fValue1, fValue2;
我尝试了 3 种不同的插入值的方法: 方法一:
myMap1.insert(std::pair<float, float>(fValue1, fValue2));
m_Mainmap.insert(std::pair<int,std::pair<float,float> >(iValue,myMap1 ));
方法二:
m_Mainmap.insert(std::pair<int,std::pair<float,float>>::value_type(iValue,fValue1, fValue2));
方法三:
myMap1.insert(std::pair<float, float>(fValue1, fValue2));
m_Mainmap.insert(std::make_pair(iValue,myMap1 ));
我写的代码没有编译。我哪里错了?
In constructor 'std::pair<_T1, _T2>::pair(const std::pair<_U1, _U2>&) [with _U1 = int, _U2 = std::map<float, float>, _T1 = const int, _T2 = std::pair<float, float>]':
error: no matching function for call to 'std::pair<float, float>::pair(const std::map<float, float>&)'
最佳答案
方法二就差不多了。您需要考虑嵌套了一对这一事实。
m_Mainmap.insert(std::pair<int, std::pair<float,float>>(i, std::pair<float,float>(fOuterRadius,fInnerRadius)));
或者
m_Mainmap.insert(std::make_pair(i, std::make_pair(fOuterRadius,fInnerRadius)));
只要您知道插入函数和此运算符之间的区别,还请考虑以下内容。 (如果键已经存在,则插入不会更新值)
m_Mainmap[i] = std::pair<float,float>(fOuterRadius,fInnerRadius);
我不知道你的std::map<float,float>
是为了,因为您从未在问题陈述中详细说明。
关于c++ - 如何将浮点值插入到 C++ 中的映射中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28964242/