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 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝