class Caretaker {
public:
Memento& State(const std::string& s) { return mementos_.at(s); }
void SetState(const std::string& s, const Memento& m) {
mementos_.try_emplace(s, m);
}
private:
std::unordered_map<std::string, Memento> mementos_;
};
int main() {
Caretaker caretaker;
Originator originator(1);
caretaker.SetState("one", originator.ValueMemento());
originator.SetValue(2);
caretaker.SetState("two", originator.ValueMemento());
originator.SetValue(3);
caretaker.SetState("three", originator.ValueMemento());
originator.Load(caretaker.State("two"));
originator.PrintValue(); // 2
originator.Load(caretaker.State("one"));
originator.PrintValue(); // 1
originator.Load(caretaker.State("three"));
originator.PrintValue(); // 3
}