CS106L Winter2025 Assignment2 Marriage Pact

关于CS106L的第二个作业的个人解法

写在前面

本作业取自Stanford CS106L Winter 2025 第2个Assignment。主要考察对读取文件内容、容器等内容的考察,总体难度不大,但是有几个坑,笔者会在下方标注坑处,如果你的测试过不去,大概率就是这里出了问题。

具体思路

kYourName

将上方的名字改成自己的

1
std::string kYourName = "YuTaki X";

get_applicants

本题需要从一个.txt文件中读取其中内容,并将其中内容放到一个set里,具体思路即为

  1. 打开文件
  2. 读取每行的内容
  3. 将此内容放到set中
  4. 直到没有内容可读取 可以写出
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
std::set<std::string> get_applicants(std::string filename) {
  std::set<std::string> students;
  std::ifstream inputFile;
  inputFile.open(filename);
  if (inputFile.is_open()) {
    std::string line;
    while (std::getline(inputFile, line)) {
      students.insert(line);
    }
  }
  inputFile.close();
  return students;
}

find_matches

本体需要让我们找出与name有相同initials名字的人,并将其指针加入到一个queue中

坑点1 什么是initials? 比如你的名字是Zhang San,那么initials就是ZS

具体思路为

  1. 写一个辅助函数,用于得到每个名字的initials
  2. 迭代查询set里的每个元素,若有与之相同的,则将其指针加入queue中 你需要知道的内容有
  • 如何获取一个字符串中每个单词的第一个字母

坑点2 在用stringstream时,要在前面include <sstream>

  • 如何迭代set
  • 如何表示一个指针
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <sstream>
std::string helper(std::string name) {
  std::stringstream ss;
  ss << name;
  std::string first;
  std::string last;
  ss >> first >> last;
  std::string res;
  res += first[0];
  res += last[0];
  return res;
}

std::queue<const std::string*> find_matches(std::string name, std::set<std::string>& students) {
  std::queue<const std::string*> res;
  for (const auto& student : students) {
    if (helper(name) == helper(student)) {
      res.push(&student);
    }
  }
  return res;
}

get_match

选择你自己喜欢的方法来从queue中再进一步得到一个特殊的名字(我的名字已经够特殊的了,就不再写了,其实是懒)

1
2
3
4
5
6
7
std::string get_match(std::queue<const std::string*>& matches) {
  if (!matches.empty()) {
    return *matches.front();
  } else {
    return "NO MATCHES FOUND.";
  }
}

写在后面

总体不难,都是些小问题容易恼火 GitHub地址

不会摄影的药师不是好程序员
使用 Hugo 构建
主题 StackJimmy 设计