当前位置: 首页 > news >正文

CS106L:Assignment 6:Explore Courses 作业

作业要求

  1. 在CourseDatabase类中实现find_course,在成员变量courses中寻找与传入的字符串course_title相同的course,返回std::optional<Course>
  2. 实现main函数中,利用Monadic,将std::optional<Course>转化为std::string,Course包含值则返回"Found course: title,number_of_units,quarter\n",否则返回"Course not found.\n"

问题及解决

and_then transform or_else的具体写法

std::optional<Course>course;course.and_then([](constCourse&crs)->std::optional<std::string>{return...});course.transform([](constCourse&crs)->std::string{return...});course.or_else([]()->std::optional<std::string>{return...});

注意各个lambda函数的传入的参数和返回值

format的使用

对于"Found course: title,number_of_units,quarter\n"这种字符串打印需求,可以使用std::format

std::format("Found course: {},{},{}\n",crs.title,crs.number_of_units,crs.quarter);

main.cpp代码

/* * CS106L Assignment 6: Explore Courses * Created by Haven Whitney with modifications by Jacob Roberts-Baca and Fabio * Ibanez. */#include<algorithm>#include<type_traits>#include<vector>#include<optional>#include"autograder/utils.hpp"/** * A course. This should be familiar from Assignment 1! */structCourse{std::string title;std::string number_of_units;std::string quarter;/** * You don't have to ignore this anymore! We're defining the `==` operator for * the Course struct. */booloperator==(constCourse&other)const{returntitle==other.title&&number_of_units==other.number_of_units&&quarter==other.quarter;}};classCourseDatabase{public:CourseDatabase(std::string filename){autolines=read_lines(filename);std::transform(lines.begin(),lines.end(),std::back_inserter(courses),[](std::string line){autoparts=split(line,',');returnCourse{parts[0],parts[1],parts[2]};});}/** * Finds a course in the database with the given title, if it exists. * @param course_title The title of the course to find. * @return You will need to figure this out! */std::optional<Course>find_course(std::string course_title){for(constauto&elem:courses){if(elem.title==course_title){returnelem;}}return{};}private:std::vector<Course>courses;};intmain(intargc,char*argv[]){static_assert(!std::is_same_v<std::invoke_result_t<decltype(&CourseDatabase::find_course),CourseDatabase,std::string>,FillMeIn>,"You must change the return type of CourseDatabase::find_course to ""something other than FillMeIn.");if(argc==2){CourseDatabasedb("autograder/courses.csv");autocourse=db.find_course(argv[1]);/******************************************************** STUDENT_TODO: Populate the output string with the right information to print Please pay special attention to the README here ********************************************************/std::string output=course.and_then([](constCourse&crs)->std::optional<std::string>{returnstd::format("Found course: {},{},{}\n",crs.title,crs.number_of_units,crs.quarter);}).or_else([]()->std::optional<std::string>{return"Course not found.\n";}).value();/******************************************************** DO NOT MODIFY ANYTHING BELOW THIS LINE PLEASE ********************************************************/std::cout<<output<<std::endl;return0;}returnrun_autograder();}
http://www.jsqmd.com/news/601215/

相关文章:

  • 野人先生联系方式查询:关于品牌官方联系渠道获取与产品体验的实用指南 - 品牌推荐
  • 三步解决华硕笔记本性能优化难题:G-Helper全方位调控指南
  • LongCat-Image-Editn一文详解:6B小模型如何实现开源图像编辑SOTA
  • 终极解决方案:高效修复Kindle电子书封面的完整指南
  • MediaCreationTool.bat:解决Windows 11安装难题的7大突破方案
  • MogFace-large开源模型部署教程:适配国产昇腾/寒武纪AI芯片可行性分析
  • FRCRN与ComfyUI工作流集成:构建可视化语音处理管道
  • Driver Store Explorer深度指南:释放Windows系统空间的智能驱动管理方案
  • G-Helper完整指南:华硕笔记本的终极轻量级控制工具
  • 2026年靠谱的高温热风枪/德士热风枪/香港便携式热风枪优质厂家推荐汇总 - 行业平台推荐
  • seo网站推广服务公司如何分析竞争对手_seo网站推广服务公司如何进行关键词优化
  • 5步实现跨平台音乐自由:开源格式转换工具完整指南
  • 破局蓝桥杯:算法基础三剑客“枚举、模拟、贪心”的底层逻辑与实战心法
  • MPC模型预测控制系列之C++实现
  • CSS Filters:图像效果的魔法
  • 告别会议记录烦恼:5分钟掌握Windows实时语音转文字神器
  • 视觉拼图微调:突破MLLM细粒度理解瓶颈,Day31_【 NLP _1.文本预处理 _(4)文本特征处理、文本数据增强】。
  • Phi-4-Reasoning-Vision惊艳案例:气象云图+传感器数据→灾害预警推理过程
  • 阿里通义Z-Image模型体验:低显存运行,效果惊艳实测
  • 如何破解网易云音乐加密限制?ncmdump让音乐文件自由播放
  • 基于MATLAB的边缘检测系统开发包|含完整源码、PPT课件、实验报告与参考文献
  • 四川鑫诚固德立体仓库货架系统帮你实现仓库智能化!
  • CLIP-GmP-ViT-L-14部署案例:纯本地无网运行的图文匹配验证方案
  • 一键部署FireRed-OCR:快速体验工业级文档解析,支持表格公式
  • TurboDiffusion保姆级教程:基于Wan2.1/Wan2.2的AI视频生成快速上手
  • G-Helper:3个核心突破重新定义华硕笔记本性能管理
  • DeepAnalyze性能优化:多线程处理实战
  • 哈尔滨海博英语联系方式查询:关于语言培训机构选择与联系方式的通用指南与客观背景介绍 - 品牌推荐
  • 中兴机顶盒三码修改工具|支持MAC/SN/STBID一键批量改码
  • Qwen3.5-9B-AWQ-4bit部署全流程:从环境配置到Web界面访问