trainings/AdvancedCppV2/Presentation/exercises/map/map.cpp

54 lines
1.2 KiB
C++

#include <algorithm>
#include <iostream>
#include <type_traits>
#include <vector>
// insert [] at
template <typename Key, typename Value>
class vecMap {
std::vector<std::pair<Key,Value>> container;
public:
Value at(const Key &key) {
for(std::pair<Key, Value> p : container){
if(p.first == key) return p.second;
}
// throw exception
throw
}
// U& operator[](T key) {
// auto it = find(v_key.begin(), v_key.end(), key);
// if (it != v_key.end()) {
// int index = it - v_key.begin();
// return v_value[index];
// }
// v_key.push_back(key);
// v_value.push_back(default_value);
// return v_value.back();
// }
Value &operator[](Key &key){
for(std::pair<Key, Value> p : container){
if(p.first == key) return p.second;
}
container.push_back(std::make_pair(key, 0));
return container.back().second;
}
void insert(Key k, Value v){
container.push_back(std::make_pair(k, v));
}
};
int main(){
vecMap<int, char> map;
map.insert(1,'a');
map[1] = 'e';
std::cout << map[1];
map.at(2);
}