#include #include #include #include template class VectorMap { std::vector keys_; std::vector values_; static_assert(std::is_default_constructible::value, "Value should have the default constructor"); static_assert(std::is_default_constructible_v, "Value should have the default constructor"); public: using K = Key; using V = Value; template using iterator = typename std::vector::iterator; void insert(const Key& k, const Value& v) { keys_.emplace_back(k); values_.emplace_back(v); } static constexpr bool isIntKey() { // if (std::is_integral_v) { if (std::is_same_v) { return true; } return false; } Value& operator[](const Key& k); Value& at(const Key& k); static constexpr bool is_int_key = std::is_same_v; }; 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]; } template class VectorMap { // specialization static_assert(std::is_default_constructible::value, "ValueType must have the default constructor"); ValueType t_; ValueType f_; public: static constexpr bool is_int_key = false; // type_traits #2 VectorMap() { std::cout << "bool specialization used\n"; } void insert(bool key, ValueType&& value) { if (key) { t_ = value; } else { f_ = value; } } ValueType& operator[](bool key) { if (key) { return t_; } return f_; } ValueType& at(bool key) { if (key) { return t_; } return f_; } }; int main() { VectorMap map; // VectorMap::iterator k; map.insert(1, 'c'); std::cout << map[1] << '\n'; map[1] = 'e'; // replaces value under 1 std::cout << map[1] << '\n'; // prints 'e' // map.at(2); // throw std::out_of_range std::cout << std::boolalpha << map.isIntKey() << '\n'; std::cout << std::boolalpha << map.is_int_key << '\n'; VectorMap map3; std::cout << std::boolalpha << map3.isIntKey() << '\n'; VectorMap map4; map4.insert(true, 3); map4.insert(false, 0); std::cout << map4[true] << '\n'; map4[true] = 10; // replaces value under 1 std::cout << map4[true] << '\n'; // prints 'e' std::vector v{1, 2, 3}; find(begin(v), end(v), 2); return 0; }