使用Maps和make_pair编译错误

时间:2015-05-31 04:20:18

标签: c++ stl

我试图了解一些关键的STL概念。我编写了以下程序来了解std::pairmaps的工作情况,但我收到了一个令人讨厌的编译错误。尝试添加所有标题,但是,到目前为止无法弄清楚什么是错误的。请帮忙。

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

我得到的编译错误:

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>          ]

1 个答案:

答案 0 :(得分:2)

有两个错误。

  • 第一个是在这一行:

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

    地图内元素的类型是std::pair,其中包含键(名称)和值(电话号码)。如果您想使用std::find(),则需要为其提供您正在寻找的确切值对,而不仅仅是密钥。

    可以使用std::map::find()来代替搜索密钥:

    DBIT_Type it = phoneDb.find(name);
    
  • 第二个是在这一行:

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

    此操作失败,因为phint,但地图的密钥是std::string。您显然正在尝试search an element by its value(电话号码)而不是其密钥(名称)。