0

I'm trying to brush up on a few of the key STL concepts. I wrote the below program to understand working of std::pair and maps, but I'm getting a nasty compile error. Tried adding all the headers as well but, unable to figure out whats wrong so far. Please help.

void testMaps()
{
    DB_Type phoneDb;
    PhoneInfoList_Type v1(8);
    PhoneInfo_Type(*phoneInfoGenerator)() = phoneInfoGen;
    DBIT_Type it;
    PhoneInfoList_Type::iterator phit;
    int i = 0, j = 0;

    for (phit = v1.begin(); phit != v1.end(); ++phit)
    {
        v1.push_back(phoneInfoGen());
    }

    for (phit = v1.begin(); phit != v1.end(); ++phit)
    {
        phoneDb.insert(*phit);
    } 
    int choice;
    std::cout << "Enter choice 0. Search by name, 1. Search by no., 2. Exit\n";
    while (1)
    {
        switch (choice)
        {
            case 0:
            {
                std::cout << "Enter name:";
                std::string name;
                std::cin >> name;
                DBIT_Type it = 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 it = std::find(phoneDb.begin(), phoneDb.end(), ph);
                if (it != phoneDb.end())
                {
                    std::cout << "\n Name of person with phone no " << ph << "is " << it->first << "\n";
                }
                else
                {
                    std::cout << "Name not found\n";
                }
            }
            break;
            default:
                break;
        }
    }
}

The compile error that I'm getting :

warning C4244: 'argument' : conversion from 'time_t' to '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 which takes a left-hand operand of type 'std::pair<const _Kty,_Ty>' (or there is no acceptable conversion)
1>          with
1>          [
1>              _Kty=std::string
1>  ,            _Ty=int
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\system_error(410): could be '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 to match the argument list '(std::pair<const _Kty,_Ty>, const std::string)'
1>          with
1>          [
1>              _Kty=std::string
1>  ,            _Ty=int
1>          ]
Pang
  • 9,564
  • 146
  • 81
  • 122
vinit
  • 501
  • 5
  • 15
  • Next time, try to post code that is compilable as-is and include all the relevant error messages. – isanae May 31 '15 at 04:31

1 Answers1

2

There are two errors.

  • The first one is on this line:

    DBIT_Type it = std::find(phoneDb.begin(), phoneDb.end(), name);
    

    The type of an element inside a map is an std::pair, which contains both the key (name) and the value (phone number). If you want to use std::find(), you need to give it the exact pair of values you are looking for, not merely the key.

    Searching for the key can be done by using std::map::find() instead:

    DBIT_Type it = phoneDb.find(name);
    
  • The second one is on this line:

    DBIT_Type it = std::find(phoneDb.begin(), phoneDb.end(), ph);
    

    This fails because ph is an int, but the map's key is an std::string. You are apparently trying to search an element by its value (the phone number) instead of by its key (the name).

Community
  • 1
  • 1
isanae
  • 3,253
  • 1
  • 22
  • 47
  • 1
    A BidirectionalIterator satisfies all the requirements of InputIterators (and more) so you definitely can pass those to `std::find`. And `std::pair` provides `operator==`. The error in the OP's example is that he's trying to call `find` with either the `key_type` or `mapped_type` as the value to be found, when the only way to get it to work is to pass an instance of the `value_type` (`std::pair`) to `find`. Anyway, he should use `map::find` as you suggest. – Praetorian May 31 '15 at 04:45