c++ - Compile error with Maps and make_pair -


i'm trying brush on few of key stl concepts. wrote below program understand working of std::pair , maps, i'm getting nasty compile error. tried adding headers but, unable figure out whats wrong far. please help.

void testmaps() {     db_type phonedb;     phoneinfolist_type v1(8);     phoneinfo_type(*phoneinfogenerator)() = phoneinfogen;     dbit_type it;     phoneinfolist_type::iterator phit;     int = 0, j = 0;      (phit = v1.begin(); phit != v1.end(); ++phit)     {         v1.push_back(phoneinfogen());     }      (phit = v1.begin(); phit != v1.end(); ++phit)     {         phonedb.insert(*phit);     }      int choice;     std::cout << "enter choice 0. search name, 1. search no., 2. exit\n";     while (1)     {         switch (choice)         {             case 0:             {                 std::cout << "enter name:";                 std::string name;                 std::cin >> name;                 dbit_type = std::find(phonedb.begin(), phonedb.end(), name);                 if (it != phonedb.end())                 {                     std::cout <<"\n phone no of " << name << "is " << it->second << "\n";                 }                 else                 {                     std::cout << "name not found\n";                 }             }break;             case 1:             {                 std::cout << "enter phone no:";                 int ph;                 std::cin >> ph;                 dbit_type = std::find(phonedb.begin(), phonedb.end(), ph);                 if (it != phonedb.end())                 {                     std::cout << "\n name of person phone no " << ph << "is " << it->first << "\n";                 }                 else                 {                     std::cout << "name not found\n";                 }             }             break;             default:                 break;         }     } } 

the compile error i'm getting :

warning c4244: 'argument' : conversion 'time_t' 'unsigned int', possible loss of data 1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(3026): error c2678: binary '==' : no operator found takes left-hand operand of type 'std::pair<const _kty,_ty>' (or there no acceptable conversion) 1>          1>          [ 1>              _kty=std::string 1>  ,            _ty=int 1>          ] 1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\system_error(410): 'bool std::operator ==(const std::error_condition &,const std::error_code &) throw()' 1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\system_error(402): or       'bool std::operator ==(const std::error_code &,const std::error_condition &) throw()' 1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\exception(507): or       'bool std::operator ==(const std::exception_ptr &,std::nullptr_t)' 1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\exception(502): or       'bool std::operator ==(std::nullptr_t,const std::exception_ptr &)' 1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\exception(497): or       'bool std::operator ==(const std::exception_ptr &,const std::exception_ptr &)' 1>          while trying match argument list '(std::pair<const _kty,_ty>, const std::string)' 1>          1>          [ 1>              _kty=std::string 1>  ,            _ty=int 1>          ] 

there 2 errors.

  • the first 1 on line:

    dbit_type = std::find(phonedb.begin(), phonedb.end(), name); 

    the type of element inside map std::pair, contains both key (name) , value (phone number). if want use std::find(), need give exact pair of values looking for, not merely key.

    searching key can done using std::map::find() instead:

    dbit_type = phonedb.find(name); 
  • the second 1 on line:

    dbit_type = std::find(phonedb.begin(), phonedb.end(), ph); 

    this fails because ph int, map's key std::string. apparently trying search element value (the phone number) instead of key (the name).


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -