From b57df4dceed7098fbffb82df309325ab01ef5b93 Mon Sep 17 00:00:00 2001 From: Sasza Stanczew Date: Fri, 26 Apr 2024 15:04:30 +0200 Subject: [PATCH] =?UTF-8?q?AdvC++=20-=20niekompletna=20implementacja=20bo?= =?UTF-8?q?=20nie=20zd=C4=85=C5=BCy=C5=82em?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Presentation/exercises/map/map.cpp | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/AdvancedCppV2/Presentation/exercises/map/map.cpp b/AdvancedCppV2/Presentation/exercises/map/map.cpp index 23b74e9..0b295cf 100644 --- a/AdvancedCppV2/Presentation/exercises/map/map.cpp +++ b/AdvancedCppV2/Presentation/exercises/map/map.cpp @@ -2,3 +2,53 @@ #include #include #include + +// insert [] at + +template +class vecMap { + std::vector> container; + +public: + Value at(const Key &key) { + for(std::pair 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 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 map; + map.insert(1,'a'); + map[1] = 'e'; + std::cout << map[1]; + map.at(2); + +}