CS106L assignment2 Marriage Pact
给一个姓名表,取首字母作匹配,将姓名表存入set里面 ,然后从set里遍历缩写,和函数给出的name对应的缩写相同的话则放入队列里(这里队列里存的string指针),队列是可能匹配的人,然后随一个出来
用指针的话 对于string可能很长,指针则只会操作地址而非字符串,加快速度,空间时间上都更好 引用的都是同一份数据
/* * 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. * */ #include <fstream> #include <iostream> #include <queue> #include <set> #include <string> #include <unordered_set> #include <random> std::string kYourName = "XuanRui Liu"; // Don't 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 applicant's 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::set<std::string> get_applicants(std::string filename) { // STUDENT TODO: Implement this function. std::set<std::string> cts; std::ifstream in(filename); std::string tmp; while(std::getline(in, tmp)) { if (tmp.empty()) { continue; } cts.insert(tmp); //std::cout<<"hello"; } in.close(); return cts; } /** * 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::queue<const std::string*> find_matches(std::string name, std::set<std::string>& students) { // STUDENT TODO: Implement this function. std::queue<const std::string*> tmp; char a = name[0], b; for(int i = 1 ; i < name.size(); i++) { if(name[i] >= 'A' && name[i] <= 'Z') { b = name[i]; } } char tmpa, tmpb; for(auto x : students) { //std::cout<<x.size()<<std::endl; tmpa = x[0]; for(int i = 1; i < x.size(); i++) { if(x[i] >= 'A' && x[i] <= 'Z') { tmpb = x[i]; } } if(a == tmpa && b == tmpb) tmp.push(&x); } return tmp; } /** * Takes in a queue of pointers to possible matches and determines the one true match! * * You can implement this function however you'd 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::string get_match(std::queue<const std::string*>& matches) { // STUDENT TODO: Implement this function. const std::string nfd = "NO MATCHES FOUND."; if(matches.empty()) return nfd; std::random_device rd; std::mt19937 gen(rd()); std::uniform_int_distribution<> dis(0, matches.size() - 1); int rdm = dis(gen); while(rdm--) { matches.pop(); } return *(matches.front()); } /* #### Please don't remove this line! #### */ #include "autograder/utils.hpp"