0

コーディングする必要があるテンプレートのスケルトンが与えられました。テンプレートを実際に扱ったことはありません。このテンプレートの奇妙な点は、テンプレート自体内にクラスをコーディングする必要があることです。クラス関数だけでなく、まったく新しいクラスです。テンプレート用のコードは次のとおりです (開始するために、いくつかのメソッド内に数行のコードを挿入しましたが、それらは私の問題にとって重要ではありません)。

含む

template<class K, class V> class HMap
{
 private:
 // insert instance variables
 // insert constants (if any)

 public:
 class MapEntry
 {
    private:
        K key;
        V value;

    public:
    /**
     * Creates a MapEntry.
     * 
     * @param akey the key
     * @param avalue the value
     */
    MapEntry(K akey, V avalue)
    {
        key = akey;
        value = avalue;
    }

    /**
     * Returns the key for this entry.
     * 
     * @return the key for this entry
     */
    K getKey()
    {
        return key;
    }

    /**
     * Returns the value for this entry.
     * 
     * @return the value for this entry
     */
    V getValue()
    {
        return value;
    }

    /**
     * Sets the value for this entry.
     * 
     * @param newValue
     * @return the previous value for this entry
     */
    V setValue(V newValue)
    {
        V oldval;
        oldval = value;
        value = newvalue;
        return oldval;
    }
};

HMap テンプレート タイプのオブジェクトを作成する場合、その中で MapEntry クラスをどのように使用しますか? 私はテンプレートにまったく慣れていないので、どこから始めればよいかわかりません。ありがとうございました。

4

1 に答える 1

0

HMap<Int, Double>::MapEntry内部クラスを参照します。しかし、なぜ内部構造を公開したいのですか?

たぶんそれはもっと簡単でしょう(私は怠惰なので、ハッシュではなくペアのリストを実装します)

template <type Key, type Value>
class HashMap {
private:
struct entry { Key key, Value value};
std::list<entry> map;
public:
  HashMap() {};
  void add_element(Key key, Value value)
    {
      entry e = {.key = key, .value = value};
      map.push_back(e);
    };
}
于 2012-12-01T11:50:20.293 に答える