gpt4 book ai didi

c++ - 使用 Maps 和 make_pair 编译错误

转载 作者:行者123 更新时间:2023-11-28 02:26:17 26 4
gpt4 key购买 nike

我正在尝试复习一些关键的 STL 概念。我编写了以下程序来理解 std::pairmaps 的工作原理,但我遇到了一个严重的编译错误。也尝试添加所有 header ,但到目前为止无法找出问题所在。请帮忙。

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

最佳答案

有两个错误。

  • 第一个在这一行:

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

    映射中元素的类型是 std::pair,其中包含键(姓名)和值(电话号码)。如果你想使用 std::find(),你需要给它你正在寻找的值对,而不仅仅是键。

    可以使用 std::map::find() 代替搜索 key :

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

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

    这会失败,因为 ph 是一个 int,但是映射的键是一个 std::string。您显然是在尝试 search an element by its value (电话号码)而不是按键(姓名)。

关于c++ - 使用 Maps 和 make_pair 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30553796/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com