Init
This commit is contained in:
86
ACMOJ-2059.h
Normal file
86
ACMOJ-2059.h
Normal file
@ -0,0 +1,86 @@
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
class Student {
|
||||
public:
|
||||
static std::map<int, Student *> id2student;
|
||||
static std::map<std::string, Student *> name2student;
|
||||
enum class Gender { Boy, Girl };
|
||||
|
||||
private:
|
||||
int id;
|
||||
std::string name;
|
||||
int age;
|
||||
Gender gender;
|
||||
|
||||
public:
|
||||
// 从输入流构造
|
||||
// 输入格式与输出格式一致
|
||||
// 输出函数已给出
|
||||
Student(std::istream &is) {
|
||||
std::string tmp_gender;
|
||||
is >> id >> name >> age >> tmp_gender;
|
||||
if (tmp_gender == "boy")
|
||||
gender = Gender::Boy;
|
||||
else
|
||||
gender = Gender::Girl;
|
||||
id2student[id] = this;
|
||||
name2student[name] = this;
|
||||
}
|
||||
|
||||
// 人无法被复制(至少不应该)
|
||||
Student(Student const &rhs) = delete;
|
||||
Student &operator=(Student const &rhs) = delete;
|
||||
// 你需要支持的操作
|
||||
Student(Student &&rhs) {
|
||||
id = rhs.id;
|
||||
name = rhs.name;
|
||||
age = rhs.age;
|
||||
gender = rhs.gender;
|
||||
rhs.id = 0;
|
||||
id2student[id] = this;
|
||||
name2student[name] = this;
|
||||
}
|
||||
Student &operator=(Student &&rhs) {
|
||||
if ((&rhs) != this) {
|
||||
if (id != 0) {
|
||||
id2student.erase(id);
|
||||
name2student.erase(name);
|
||||
}
|
||||
id = rhs.id;
|
||||
name = rhs.name;
|
||||
age = rhs.age;
|
||||
gender = rhs.gender;
|
||||
rhs.id = 0;
|
||||
id2student[id] = this;
|
||||
name2student[name] = this;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
~Student() {
|
||||
if (id != 0) {
|
||||
id2student.erase(id);
|
||||
name2student.erase(name);
|
||||
}
|
||||
}
|
||||
|
||||
Student &rename(std::string const &str) & {
|
||||
name2student.erase(name);
|
||||
name = str;
|
||||
name2student[name] = this;
|
||||
return *this;
|
||||
}
|
||||
Student rename(std::string const &str) && {
|
||||
name2student.erase(name);
|
||||
name = str;
|
||||
name2student[name] = this;
|
||||
Student tmp(std::move(*this));
|
||||
return std::move(tmp);
|
||||
}
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &os, Student &stu) {
|
||||
return os << stu.id << ' ' << stu.name << ' ' << stu.age << ' '
|
||||
<< (stu.gender == Student::Gender::Boy ? "boy" : "girl");
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user