Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- template<class Key, class Value>
- class Element {
- public:
- Key key;
- Value value;
- };
- template<class Key, class Value>
- class map {
- //friend class Element<Key, Value>;
- Element<Key, Value>* tab;
- int size; //rozmiar tablicy
- public:
- map() {
- tab = NULL;
- size = 0;
- }
- ~map() {
- if(tab != NULL){
- delete[] tab;
- tab = NULL;
- }
- size = 0;
- }
- int Size(void) {
- return this->size;
- }
- bool empty(void){
- if(size == 0)
- return true;
- return false;
- }
- map& clear(void){
- if(this->tab != NULL){
- delete[] this->tab;
- tab = NULL;
- }
- this->size = 0;
- return *this;
- }
- Value & operator [] (const Key &k) {
- if(this->size == 0){
- tab = new Element<Key, Value>[1];
- tab[0].key = k;
- size++;
- return tab[0].value;
- }
- else {
- for(int i = 0; i < this->size; i++) {
- if(this->tab[i].key == k) {
- return this->tab[i].value;
- }
- }
- Element<Key, Value>* tmp;
- tmp = new Element<Key, Value>[size];
- for(int i=0; i<this->size; i++){
- tmp[i].value = tab[i].value;
- tmp[i].key = tab[i].key;
- }
- delete[] tab;
- size += 1;
- tab = new Element<Key, Value>[size];
- for(int i=0; i<size-1; i++){
- tab[i] = tmp[i];
- }
- delete[] tmp;
- tab[size-1].key = k;
- return tab[size-1].value;
- }
- }
- class iterator{
- //friend class Element<Key, Value>;
- friend class map;
- Element<Key,Value>* pointer;
- public:
- bool operator != (const iterator& o1){
- if(this->pointer != o1.pointer) {
- return true;
- }
- return false;
- }
- iterator& operator ++ (void){
- pointer++;
- return *this;
- }
- Element<Key, Value>* operator -> (void){
- return pointer;
- }
- };
- iterator begin(void){
- iterator it;
- it.pointer = &tab[0];
- return it;
- }
- iterator end(void){
- iterator it;
- it.pointer = &tab[size];
- return it;
- }
- };
- int main(){
- map<string, string> m;
- m["aa"] = "bb";
- m["cc"] = "dd";
- string s1;
- s1 = m["cc"];
- for (map<string,string>::iterator i = m.begin(); i != m.end(); ++i)
- cout << i->key << " " << i->value << endl;
- m.clear();
- for (map<string,string>::iterator i = m.begin(); i != m.end(); ++i)
- cout << i->key << " " << i->value << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement