1.8 KiB
Linux compilation
> mkdir build
> cd build
> cmake -DCMAKE_BUILD_TYPE=Debug ..
> make
Exercise 1
Write a template class VectorMap that represents an over-engineered std::map.
Inside, it should hold 2 std::vectors of the same size, each with different types. The first vector should hold keys, the other one values.
Elements at the same position in both vectors should create a pair like 1 and 'c' below.
VectorMap<int, char> map;
map.insert(1, 'c');
map[1] = 'e'; // replaces value under 1
std::cout << map[1]; // prints 'e'
map.at(2); // throw std::out_of_range
Implement the mentioned above insert(), operator[], at() methods.
Do not bother about duplicated keys for now.
You can also try to implement additional methods from the std::map interface 🙂
Use cppreference.
Exercise 2 - static_assert
Add a constraint to our VectorMap.
Do not allow to create an object when ValueType does not have a default constructor.
Use static_assert and proper trait from <type_traits> library.
Check if it works properly.
Exercise 3 - isIntKey()
Write a function isIntKey() in VectorMap. It should return true when the KeyType is int and false otherwise.
Check the <type_traits> library for some inspiration 🙂
Exercie 4
Write a partial specialization of VectorMap for boolean keys. We can have only 2 values for boolean keys. There is no need to keep vectors inside.
Implement properly all currently available functions.
Exercie 5
Write a variable template is_int_key_v. It should return a value of the is_int_key field in a given template type.