map class index operator

so i have no idea how to write the index operator overloading

i have a class called pair already.
i have insert function on my bst

#ifndef MAP_H
#define MAP_H
#include <iostream>
#include "bst.h"
#include "pair.h"

template<class T, class A>
class MapIterator;

template<class T , class A>
class Map
{
public:
Map() :items(0){}
~Map() {clear();}
inline bool empty(){if (items == 0)return true;else return false;}
inline int size() {return items;}
inline void clear() {bst.clear(); items = 0;}
A & operator [] (const T &rhs);

Map & operator = (const Map & rhs)
{
bst = rhs.bst;
items = rhs.items;
return *this;
}

private:
BST<Pair<T,A> > bst;
int items;



};

#endif


and i am trying to insert items like this

void testAdd()
{
#ifdef TEST2
// create
cout << "Create an integer-string Map\n";
Map <int, string> m1;
Map <int, string> m2;
cout << "\tEmpty? " << (m1.empty() ? "yes" : "no") << endl;
cout << "\tCount: " << m1.size() << endl;

// fill
cout << "Fill with 10 values\n";
// m1[8] = string("eight"); // 8
// m1[4] = string("four"); // +----+----+
// m1[12] = string("twelve"); // 4 12
// m1[2] = string("two"); // +--+--+ +--+--+
// m1[6] = string("six"); // 2 6 9 13
// m1[9] = string("nine"); // +-+ +-+ +-+
// m1[13] = string("thirteen"); // 0 5 11
// m1[0] = string("zero");
// m1[5] = string("five");
// m1[11] = string("eleven");

#endif // TEST2
}
Last edited on
travel down the tree, find key operator argument and return corresponding value.
If key is not found, insert [key, <default constructed value>] into your tree and return reference to newly created value.
I tried but I can't even make a pair and insert it into the bst

I tried to say Pair<T,A> (rhs,A) but of course it doesn't work. How to I make a pair with the second items? Like I know the theory of it but I just have no idea how it even work with the syntax and I am totally stuck.
Topic archived. No new replies allowed.