Charles_Su 2016-05-01 08:42 采纳率: 21.4%
浏览 2144

用STL的map和算法的find_if实现名表表示和查找功能

#include
#include
#include
#include
using namespace std;
bool older_than_20(int birthdate)
{
return (2016 - birthdate / 10000)>20;
}
int main()
{
maptable_item;//创建一个map类容器,用于存储名表
//创建名表
table_item["Charles"] = 19960207;
table_item["Sam"] = 19970130;
table_item["Michael"] = 19951213;
table_item["Benjamin"] = 19930516;
table_item["Kim"] = 19990315;
table_item["Jacob"] = 19970625;
table_item["Robinson"] = 19960322;
table_item["Lin"] = 19970331;
//输出名表
cout << "名表信息如下:\n";
map::const_iterator it;//创建一个不能修改所指向的元素的迭代器
for (it = table_item.begin(); it != table_item.end(); it++)//遍历容器
cout << it->first << ": " << it->second << endl;//输出元素的关键字和值
map::const_iterator ilocation;//创建一个不能修改所指向的元素的迭代器
ilocation =find_if(table_item.begin(),table_item.end(),older_than_20);//为什么这会报错
if (ilocation == table_item.end())
cout << "没有找到" << endl;
else
{
cout << "年龄大于20岁的人有:" << endl;
cout << ilocation->first << ": " << ilocation->second << endl;
}

system("pause");
return 0;

}

  • 写回答

1条回答 默认 最新

  • oyljerry 2016-05-01 14:16
    关注

    find_if就是实现自己对我过滤条件函数来实现查找

    评论

报告相同问题?

悬赏问题

  • ¥15 vscode问题请教
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM