src/work/bin_heap_demo.cc
changeset 253 f45703336699
parent 172 c645f4a2a6ae
child 258 94bafec4f56f
equal deleted inserted replaced
3:79ae9c125881 4:4d2e0e7713ed
    14 // a stringek aktualis helyet sajatmagan belul.
    14 // a stringek aktualis helyet sajatmagan belul.
    15 // Egy olyan stringhez, ami meg nincsen a kupac -1 -et kell rendelnunk.
    15 // Egy olyan stringhez, ami meg nincsen a kupac -1 -et kell rendelnunk.
    16 typedef BinHeap<string, double, string_int_map> StrDoubleHeap;
    16 typedef BinHeap<string, double, string_int_map> StrDoubleHeap;
    17 
    17 
    18 class string_int_map : public map<string,int> {
    18 class string_int_map : public map<string,int> {
       
    19   typedef map<string,int> parent;
    19 public:
    20 public:
    20   int get(const string &s) {
    21   int get(const string &s) {
    21     // Bocs, ez igy gaaaany, de nem volt kedvem utananezni, hogy
    22     // Bocs, ez igy gaaaany, de nem volt kedvem utananezni, hogy
    22     // hogy is mukodik ez a map :)
    23     // hogy is mukodik ez a map :)
    23     if( count(s) == 0 ) {
    24     if( count(s) == 0 ) {
    24       operator[](s) = StrDoubleHeap::PRE_HEAP;
    25       parent::operator[](s) = StrDoubleHeap::PRE_HEAP;
    25     }
    26     }
    26     return operator[](s);
    27     return parent::operator[](s);
       
    28   }
       
    29   int operator[](const string &s) {
       
    30     return get(s);
    27   }
    31   }
    28   void set(const string &s, int i) {
    32   void set(const string &s, int i) {
    29       operator[](s) = i;
    33       parent::operator[](s) = i;
    30   }
    34   }
    31 };
    35 };
    32 
    36 
    33 
    37 
    34 int main()
    38 int main()
    35 {
    39 {
    36   string_int_map sim;
    40   string_int_map sim;
    37   
    41   
    38   
    42   
    39   cout << "testing string_int_map default value:\n";
    43   cout << "testing string_int_map default value:\n";
    40   cout << "  alma: " << sim.get("alma") << endl;
    44   cout << "  sim.get(\"alma\"): " << sim.get("alma") << endl;
       
    45   cout << "  sim[\"alma\"]: " << sim["alma"] << endl;
    41 
    46 
    42   cout << "creating the heap\n";
    47   cout << "creating the heap\n";
    43   StrDoubleHeap heap(sim);
    48   StrDoubleHeap heap(sim);
    44 
    49 
    45   cout << "heap.push(\"alma\", 15);\n";
    50   cout << "heap.push(\"alma\", 15);\n";
    48   cout << "heap.set(\"korte\", 3.4);\n";
    53   cout << "heap.set(\"korte\", 3.4);\n";
    49   heap.set("korte", 3.4);
    54   heap.set("korte", 3.4);
    50 
    55 
    51   cout << "heap.get(\"alma\") = " 
    56   cout << "heap.get(\"alma\") = " 
    52        << heap.get("alma")
    57        << heap.get("alma")
       
    58        << endl;
       
    59   cout << "heap[\"alma\"] = " 
       
    60        << heap["alma"]
    53        << endl;
    61        << endl;
    54 
    62 
    55   cout << "heap.top() = "
    63   cout << "heap.top() = "
    56        << heap.top() << endl;
    64        << heap.top() << endl;
    57   cout << "heap.topPrio() = "
    65   cout << "heap.topPrio() = "