dimanche 26 mars 2017

How to move data from one array to other C++11 ?

I have my own class Vector

class Vector
{
private:
    int* buffer;
    size_t size;
    size_t capacity;
public:
    Vector(size_t s) {
        size = 0;
        capacity = s;       
        buffer = new int[capacity];
    }
  void push_back(const int& v) {
        if (size >= capacity)
            reserve(capacity + 5); //allocate more memory
        buffer[size++] = v; //write data to array
    }
    void reserve(size_t capacity) {
        int* prev = buffer; //save the pointer
        this->capacity = capacity; //new capacity
        buffer = new int[capacity]; //allocate the memory
        //I tried this to move data from previous array to new
        //buffer = std::move(prev, prev+5, buffer);
    }

I save the pointer to previous array, allocated new, how to move (NOT COPY) data to new just allocated on heap array ?

Aucun commentaire:

Enregistrer un commentaire