使用Maps和make_pair编译错误

Compile error with Maps and make_pair

本文关键字:pair 编译 错误 make Maps 使用      更新时间:2023-10-16

我正在复习几个关键的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. Exitn";
    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 foundn";
                }
            }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 foundn";
                }
            }
            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.0vcincludexutility(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.0vcincludesystem_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.0vcincludesystem_error(402): or       'bool std::operator ==(const std::error_code &,const std::error_condition &) throw()'
1>          c:program files (x86)microsoft visual studio 12.0vcincludeexception(507): or       'bool std::operator ==(const std::exception_ptr &,std::nullptr_t)'
1>          c:program files (x86)microsoft visual studio 12.0vcincludeexception(502): or       'bool std::operator ==(std::nullptr_t,const std::exception_ptr &)'
1>          c:program files (x86)microsoft visual studio 12.0vcincludeexception(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>          ]

有两个错误

  • 第一个在这一行:

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

    map中的元素类型为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。您显然试图通过其值(电话号码)而不是通过其键(名称)搜索元素。