CS106L:Assignment 2:Marriage Pact 作业
作业要求从students.txt中读取学生名称每行代表一个名字e.g.Julieta Heath存储在std::set或者std::unordered_set中寻找与你的名字首字母缩写相同的学生名称将其字符串的索引存储在std::queueconst std::string*中任意写一个函数找到满足条件的名称中唯一的一个问题及解决vector的初始化std::vectorchar initial初始化了一个元素个数为0的vector所以不能直接使用initial.at(0)会导致超出范围报错可以在初始化时设定元素个数std::vectorchar initial(2)每个元素都默认为0或者在添加元素时使用push_back课后问题Q1std::set与std::unordered_set的优劣另外给出一个可以用来处理学生名称使其存储在unordered_set中的哈希函数。A1std::set 基于平衡二叉搜索树实现元素自动排序支持范围查询等有序操作插入/查找时间复杂度 O(log n)std::unordered_set 基于哈希表实现元素无序插入/查找平均时间复杂度 O(1)性能更快内存占用更大需要处理哈希冲突hash(name) (Σ(name[i] * 31^i) mod TableSize)Q2注意到我们在queue中存储的是指向名字的指针而不是名字本身为什么有这样的需求如果原始集合超出作用域而指针仍然指向其元素会发生什么A2避免复制整个字符串减少内存开销提高程序效率。如果原始集合被销毁指针将成为悬空指针。解引用这些指针会导致未定义行为程序崩溃或数据错误。main.cpp源码/* * CS106L Assignment 2: Marriage Pact * Created by Haven Whitney with modifications by Fabio Ibanez Jacob Roberts-Baca. * * Welcome to Assignment 2 of CS106L! Please complete each STUDENT TODO * in this file. You do not need to modify any other files. * */#includefstream#includeiostream#includequeue#includeset#includestring#includeunordered_setstd::string kYourNameZhang Yongqi;// Dont forget to change this!/** * Takes in a file name and returns a set containing all of the applicant names as a set. * * param filename The name of the file to read. * Each line of the file will be a single applicants name. * returns A set of all applicant names read from the file. * * remark Feel free to change the return type of this function (and the function * below it) to use a std::unordered_set instead. If you do so, make sure * to also change the corresponding functions in utils.h. */std::setstd::stringget_applicants(std::string filename){std::setstd::stringapplicants;std::string temp;std::ifstreamifs(filename);while(getline(ifs,temp)){applicants.insert(temp);}returnapplicants;}// get initials of a namestd::vectorcharinitials(std::string name){std::vectorcharinitial(2);initial.at(0)name.at(0);intwhitespacename.find( );initial.at(1)name.at(whitespace1);returninitial;}/* another way to implement initials using push_back std::vectorchar initials(std::string name){ std::vectorchar initial; initial.push_back(name.at(0)); int whitespace name.find( ); initial.push_back(name.at(whitespace 1)); return initial; } *//** * Takes in a set of student names by reference and returns a queue of names * that match the given student name. * * param name The returned queue of names should have the same initials as this name. * param students The set of student names. * return A queue containing pointers to each matching name. */std::queueconststd::string*find_matches(std::string name,std::setstd::stringstudents){std::queueconststd::string*matches;std::vectorcharname_initialsinitials(name);for(constautoelem:students){if((initials(elem).at(0)name_initials.at(0))(initials(elem).at(1)name_initials.at(1))){matches.push(elem);}}returnmatches;}/** * Takes in a queue of pointers to possible matches and determines the one true match! * * You can implement this function however youd like, but try to do something a bit * more complicated than a simple pop(). * * param matches The queue of possible matches. * return Your magical one true love. * Will return NO MATCHES FOUND. if matches is empty. */std::stringget_match(std::queueconststd::string*matches){intnmatches.size()/2;for(inti0;in;i){matches.pop();}return*(matches.back());}/* #### Please dont remove this line! #### */#includeautograder/utils.hpp
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2417086.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!