Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

dictionary - How to read from a map in C++?

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

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

1 Reply

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...