【问题标题】:Dynamic allocation with template使用模板动态分配
【发布时间】:2021-09-05 06:15:09
【问题描述】:

我创建了一个类Vector,其中包含两个作为模板变量的变量,我正在尝试构建这样一个字典来告诉特定学生的标记。 问题是我正在努力使用模板动态分配内存。我必须这样做没有地图或 STL 帮助。你能解释一下,如何正确分配它们。

#define DEF_CAPACITY 20
template <class U, class T>
class Vector {
protected:
    T* _data;
    U* _keys;
    int _size; //size in use
    int _capacity; //available capacity
public:
    //constructors
    Vector(int capacity = DEF_CAPACITY);
    ~Vector();
    int getSize() const { return size; }
    void insert(U key, T data);
    T operator[](U key);
};

template<class U, class T>
inline Vector<U, T>::Vector(int capacity)
{
    this->_capacity = capacity;
    this->_size = 0;
}

template<class U, class T>
Vector<U, T>::~Vector()
{
    if (_data)
        delete[] _data;
    if (_keys)
        delete[] _keys;
}

template<class U, class T>
void Vector<U, T>::insert(U key, T data)
{
    _keys[_size] = new U;
    //Maybe I have to do something like that, but all options I test doesn't work
    //keys[_size] = new U[Some size];
    _keys[_size] = key;

    _data[_size] = new T;
    _data[_size] = data;

    _size++;
}

template<class U, class T>
T Vector<U, T>::operator[](U key)
{
int index = 0;
for (int i = 0; i < size; i++)
    if (_keys[i] == key)
        index = i;
return _data[index];
}



int main() {
    Vector<string, int> grades;
    grades.insert("john", 90);
    grades.insert("marc", 100);
    grades.insert("ron", 87);
    grades.insert("lynda", 95);
    //...
    grades.insert("Tome", 93);
    //...
    cout << grades["marc"] << endl;
    cout << grades["lynda"] << endl;
    cout << grades["john"] << endl;
    return 0;
}

【问题讨论】:

  • std::vector&lt;std::pair&lt;Key, Value&gt;&gt; 或 std::map&lt;Key, Value&gt; 怎么样?
  • @Jarod42 如果你有一个可以在没有 STL 帮助的情况下解决问题的解决方案
  • 如果你不能使用 std(使用 std::string 时这似乎很奇怪),创建你自己的 Vector 和 Pair 类(或你的二叉树)。
  • 这是一个分配的项目吗?因为std::map&lt;std::string, int&gt; 就是答案。

标签: c++ templates dynamic dynamic-memory-allocation


【解决方案1】:

要分配 n 类型为 T 的元素,请使用:

T* array = new T[n];

所以为你的键和值分配空间:

// be aware: this will call the constructor for each element!!!
_keys = new U[n];
_data = new T[n];

如果你的“向量”有一个固定的容量,这应该在你的构造函数中完成:

template<class U, class T>
inline Vector<U, T>::Vector(int capacity)
    : _data(new T[capacity]);
    , _keys(new U[capacity]);
    , _size(0);
    , _capacity(capacity);
{
}

之后,您可以像这样插入键/值对:

template<class U, class T>
void Vector<U, T>::insert(const U& key, const T& data)
{
    if(_size == _capacity)
        throw std::out_of_range("no space left!!!");
    _keys[_size] = key; // copy assignment
    _data[_size] = data; // copy assignment

    _size++;
}

这种设计将不必要地为未使用的键和值调用构造函数。

您也可以使用 malloc 和 free 来防止这种情况发生,但我不建议这样做,而是使用标准库。

【讨论】:

  • 谢谢。当你说copy assignment 这是赋值运算符还是别的什么?此外,我的运算符重载 [] 有什么问题导致代码无法编译
  • 是的,这是赋值运算符之一。您的 operator[] 的问题是您使用 size 而不是 _size 但您的编译器应该告诉您这一点。如果这回答了您的问题,请记住将其标记为已接受的答案。
  • 只是一些其他提示: 1. 您的.insert(...) 应该检查具有该键的条目是否已经存在并且更新已经存在的条目或抛出异常(或将返回值更改为bool和return false 在这种情况下)。 2. 如果没有找到给定的键,你的operator[] 应该抛出异常(而不是返回第一个数据元素)
猜你喜欢
  • 2019-03-30
  • 2010-11-21
  • 2016-09-12
  • 1970-01-01
  • 2012-06-17
  • 2011-02-25
  • 1970-01-01
  • 2012-09-14
  • 1970-01-01
相关资源
最近更新 更多