如何从C ++中的地图读取? [英] How to read from a map in C++?
本文介绍了如何从C ++中的地图读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一张 std :: map< int,float,float> m_mapWheelvalue;
of 100 elements
我需要读取值.Code我使用的是以下:
I have a map of std::map<int,float,float> m_mapWheelvalue;
of 100 elements
I need to read the values .Code i am using is below:
float fvalue1,fvalue2;
std::map<double,float,float>::iterator itNewMap;
itNewMap= m_mapWheelvalue.find(20);
if(itNewMap!= m_mapWheelvalue.end())
{
fValue1 = itNewMap->second;
fValue2= itNewMap->third;
}
但是它的赋值错误!! {third not defined}
读取第三个值
请给出正确的解决方案
but its giving error !!{ third not defined } How to read third value Please give proper solution
推荐答案
以下为我编译:
#include <map>
int main(){
std::map<double,std::pair<float,float> > m_mapWheelvalue;
float fValue1,fValue2;
std::map<double,std::pair<float,float> >::iterator itNewMap;
itNewMap= m_mapWheelvalue.find(20);
if(itNewMap!= m_mapWheelvalue.end()){
fValue1 = itNewMap->second.first;
fValue2= itNewMap->second.second;
}
}
注意:
- 查看 std :: map 定义:第一个参数是键,第二个参数是条目...第三个参数是比较函数。我想你想在条目中有几个值。我选择使用一对(如果你有两个),如果你有更多的你可能想定义一个结构/类。
- 检查变量名称,有几个案例更改。
- 地图的迭代器会向您提供一对键,条目...所以itNewMap->第一个是键,itNewMap->第二个是条目。
- Look at std::map definition: first parameter is the key, second parameter is the entry... third parameter is the comparison function. I guess you wanted to have several values in the entry. I've chosen to use a pair (as you have two), if you have more you might want to define a struct/class.
- Check variable names, there are several case changes.
- The iterator to a map gets you a pair of key,entry... so itNewMap->first is the key, itNewMap->second is the entry.
这篇关于如何从C ++中的地图读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文