Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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;  
}

but its giving error !!{ third not defined } How to read third value Please give proper solution

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
271 views
Welcome To Ask or Share your Answers For Others

1 Answer

The following compiled for me:

#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;
   }
}

Notes:

  • 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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...