0

我已经实现了一个简单的类似矢量的结构如果我使用它效果很好,vector<int>或者vector<char> 但是当我使用<vector<vector<int>>它时会出错我的代码中是否有关于矢量 stl 的良好实现代码或问题?

这是我的代码

class _vector {
private:
    int _size;
    int _capacity;
    T* vc;

public:
    _vector(int size = 1) {
        _size = 0;
        _capacity = size;
        vc = new T[size];
    }
    ~_vector() {
        delete[] vc;
    }
    int size() { return _size; }
    bool empty() { return !_size; }
    void resize(int size) {
        _capacity = size;
        T* tmp = new T[size];
        for (int i = 0; i < _size; i++) tmp[i] = vc[i];
        delete[] vc;
        vc = tmp;
    }
    void clear() {
        delete[] vc;
        _capacity = 1;
        _size = 0;
        vc = new T[_capacity];
    }
    void push_back(T val) {
        if (_size == _capacity) resize(2 * _capacity);
        vc[_size++] = val;
    }
    void pop_back() {
        if (_size == 0) return;
        vc[--_size] = 0;
    }
    T& operator[](int i) const { return vc[i]; }
    _vector<T>& operator=(_vector<T> &tmp) {
        _capacity = tmp._capacity;
        _size = tmp._size;
        delete[] vc;
        vc = new T[_capacity];
        for (int i = 0; i < _size; i++) vc[i] = tmp[i];
        return *this;
    }
4

1 回答 1

0

您的实现不遵循3 规则,因为它缺少复制构造函数和正确的复制赋值运算符(可以使用复制构造函数实现)。而在 C++11 及更高版本中,规则 5,通过添加移动构造函数和移动赋值运算符。

此外,您的实现不能与定义了构造函数/析构函数的非平凡类型一起正常工作,例如何时T是另一种_vector类型,或者在其中分配了指针/资源的任何其他类型。因此,您的类需要在向数组中添加元素时构造新对象,在placement-new从数组中删除元素时使用和破坏对象,方法是直接调用它们的析构函数。

尝试更多类似的东西:

template <typename T>
class _vector {
public:
    typedef unsigned int size_type;
    typedef T value_type;

private:
    size_type _size;
    size_type _capacity;
    value_type* vc;

public:
    _vector(size_type initalcap = 0) : _size(0), _capacity(0), vc(0) {
        reserve(initialcap);
    }

    _vector(const _vector<T> &src) : _size(0), _capacity(0), vc(0) {
        reserve(src._capacity);
        for(size_type i = 0; i < src._size; ++i) {
            new(vc[i]) value_type(src.vc[i]);
        }
        _size = src._size;
    }

    // C++11 and later only...
    _vector(_vector<T> &&src) : _size(src._size), _capacity(src._capacity), vc(src._vc) {
        src._size = 0;
        src._capacity = 0;
        src.vc = 0;
    }

    ~_vector() {
        clear();
        delete[] reinterpret_cast<char*>(vc);
    }

    size_type size() const { return _size; }
    size_type capacity() const { return _capacity; }
    bool empty() const { return !_size; }

    void reserve(size_type newcap) {
        if (newcap <= _capacity) return;
        value_type* tmp = reinterpret_cast<value_type*>(new char[sizeof(value_type) * newcap]);
        for (size_type i = 0; i < _size; ++i) {
            new(tmp[i]) value_type(vc[i]);
        }
        delete[] reinterpret_cast<char*>(vc);
        vc = tmp;
        _capacity = newcap;
    }

    void resize(size_type newsize) {
        if (newsize < _size) {
            for(size_type i = _size; i-- > newsize; ) {
                vc[i].~value_type();
            }
            _size = newsize;
        }
        else if (newsize > _size) {
            reserve(newsize);
            for (size_type i = _size; i < newsize; ++i) {
                 new(vc[i]) value_type();
            }
            _size = newsize;
        }
    }

    void clear() {
        resize(0);
    }

    void push_back(const T &val) {
        if (_size == _capacity) reserve(2 * _capacity);
        new(vc[_size]) value_type(val);
        ++_size;
    }

    void pop_back() {
        if (_size) {
            vc[--_size].~value_type();
        }
    }

    value_type& operator[](size_type i) { return vc[i]; }
    const value_type& operator[](size_type i) const { return vc[i]; }

    _vector<T>& operator=(const _vector<T> &rhs) {
        if (&rhs != this) {
            _vector<T> tmp(rhs);
            std::swap(tmp.vc, vc);
            std::swap(tmp._size, _size);
            std::swap(tmp._capacity, _capacity);
        }
        return *this;
    }

    // C++11 and later only...
    _vector<T>& operator=(_vector<T> &&rhs) {
        _vector<T> tmp(std::move(rhs));
        std::swap(tmp.vc, vc);
        std::swap(tmp._size, _size);
        std::swap(tmp._capacity, _capacity);
        return *this;
    }
};
于 2019-03-13T04:35:48.967 回答