#include #include #include #include template class VectorMap { std::vector keys_; std::vector values_; public: using K = Key; using V = Value; void insert(const Key& k, const Value& v) { keys_.emplace_back(k); values_.emplace_back(v); } Value& operator[](const Key& k); Value& at(const Key& k); }; template Value& VectorMap::operator[](const Key& k) { auto it = std::find(std::begin(keys_), std::end(keys_), k); if (it == std::end(keys_)) { keys_.emplace_back(k); values_.emplace_back(Value{}); return values_.back(); } auto dist = std::distance(keys_.begin(), it); return values_[dist]; } template Value& VectorMap::at(const Key& k) { auto it = std::find(std::begin(keys_), std::end(keys_), k); if (it == std::end(keys_)) { throw std::out_of_range("This key does not exist"); } auto dist = std::distance(keys_.begin(), it); return values_[dist]; }