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就是实现自己对我过滤条件函数来实现查找

    评论

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用