AdvC++ - niekompletna implementacja bo nie zdążyłem
This commit is contained in:
parent
80b9b35508
commit
b57df4dcee
1 changed files with 50 additions and 0 deletions
|
|
@ -2,3 +2,53 @@
|
|||
#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);
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue