Simple C++ STL vector implementation(need criticism)












0














Here is a simple version of STL vector. I want to check if I implement it correctly but STL source code is not very readable for me. Any suggestion is welcomed!



namespace nonstd {

template<typename Ty>
class vector
{
public:
using iterator = Ty * ;
using const_iterator = const Ty*;

vector();
explicit vector(const size_t count);
vector(const size_t count, const Ty& val);
vector(const vector& other);
vector(vector&& other);
~vector();

vector& operator=(const vector& other);
vector& operator=(vector&& other);

size_t size() const;
size_t capacity() const;

void push_back(const Ty& val);
void push_back(Ty&& val);
void pop_back();

Ty& front();
const Ty& front() const;
Ty& back();
const Ty& back() const;
Ty& operator(const size_t pos);
const Ty& operator(const size_t pos) const;

iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
private:
Ty * buffer;
iterator m_first;
iterator m_last;
iterator m_end;

void realloc(const size_t factor, const size_t carry);
void alloc(const size_t cap);
};

template<typename Ty>
vector<Ty>::vector() : buffer(new Ty[10]), m_first(buffer), m_last(buffer), m_end(buffer + 10) {

}

template<typename Ty>
vector<Ty>::vector(const size_t count) : buffer(new Ty[count]), m_first(buffer), m_last(buffer + count), m_end(buffer + count) {

}

template<typename Ty>
vector<Ty>::vector(const size_t count, const Ty& val) : buffer(new Ty[count]), m_first(buffer), m_last(buffer + count), m_end(buffer + count) {
while (count--) {
buffer[count] = val;
}
}

template<typename Ty>
vector<Ty>::vector(const vector& other) : buffer(new Ty[other.capacity()]), m_first(buffer), m_last(buffer + other.size()), m_end(buffer + other.capacity()) {
for (size_t i = 0; i < size(); ++i) {
buffer[i] = other[i];
}
}

template<typename Ty>
vector<Ty>::vector(vector&& other) : buffer(other.buffer), m_first(other.m_first), m_last(other.m_last), m_end(other.m_end) {
other.buffer = nullptr;
other.m_first = other.m_last = other.m_end = nullptr;
}

template<typename Ty>
vector<Ty>::~vector() {
if (buffer != nullptr) {
m_first = m_last = m_end = nullptr;
delete buffer;
}
}

template<typename Ty>
vector<Ty>& vector<Ty>::operator=(const vector<Ty>& other) {
if (this == &other) {
return *this;
}
this->~vector();
buffer = new Ty[other.capacity()];
m_first = buffer;
m_last = buffer + other.size();
m_end = buffer + other.capacity();
for (size_t i = 0; i < size(); ++i) {
buffer[i] = other[i];
}
return *this;
}

template<typename Ty>
vector<Ty>& vector<Ty>::operator=(vector<Ty>&& other) {
if (this == &other) {
return *this;
}
this->~vector();

buffer = other.buffer;
m_first = other.m_first;
m_last = other.m_last;
m_end = other.m_end;

other.buffer = nullptr;
other.m_first = other.m_last = other.m_end = nullptr;
return *this;
}

template<typename Ty>
size_t vector<Ty>::size() const {
return static_cast<size_t>(m_last - m_first);
}

template<typename Ty>
size_t vector<Ty>::capacity() const {
return static_cast<size_t>(m_end - m_first);
}

template<typename Ty>
void vector<Ty>::push_back(const Ty& val) {
if (size() < capacity()) {
*(m_last++) = val;
return;
}
realloc(2, 2);
*(m_last++) = val;
}

template<typename Ty>
void vector<Ty>::push_back(Ty&& val) {
if (size() < capacity()) {
*(m_last++) = std::move(val);
return;
}
realloc(2, 2);
*(m_last++) = std::move(val);
}

template<typename Ty>
void vector<Ty>::pop_back() {
if (size() == 0) {
throw std::exception("vector is empty");
}
(--m_last)->~Ty();
}

template<typename Ty>
Ty& vector<Ty>::front() {
if (size() == 0) {
throw std::exception("front(): vector is empty");
}
return *begin();
}

template<typename Ty>
const Ty& vector<Ty>::front() const {
if (size() == 0) {
throw std::exception("front(): vector is empty");
}
return *begin();
}

template<typename Ty>
Ty& vector<Ty>::back() {
if (size() == 0) {
throw std::exception("back(): vector is empty");
}
return *(end() - 1);
}

template<typename Ty>
const Ty& vector<Ty>::back() const {
if (size() == 0) {
throw std::exception("back(): vector is empty");
}
return *(end() - 1);
}

template<typename Ty>
Ty& vector<Ty>::operator(const size_t pos) {
if (pos >= size()) {
throw std::exception("index out of range");
}
return buffer[pos];
}

template<typename Ty>
const Ty& vector<Ty>::operator(const size_t pos) const {
if (pos >= size()) {
throw std::exception("index out of range");
}
return buffer[pos];
}

template<typename Ty>
typename vector<Ty>::iterator vector<Ty>::begin() {
return m_first;
}

template<typename Ty>
typename vector<Ty>::iterator vector<Ty>::end() {
return m_last;
}

template<typename Ty>
typename vector<Ty>::const_iterator vector<Ty>::begin() const {
return m_first;
}

template<typename Ty>
typename vector<Ty>::const_iterator vector<Ty>::end() const {
return m_last;
}

template<typename Ty>
void vector<Ty>::realloc(const size_t factor, const size_t carry) {
alloc(capacity() * factor + carry);
}

template<typename Ty>
void vector<Ty>::alloc(const size_t cap) {
Ty* new_buffer = new Ty[cap];
size_t sz = size();
for (size_t i = 0; i < sz; ++i) {
new_buffer[i] = buffer[i];
}
this->~vector();
buffer = new_buffer;
m_first = buffer;
m_last = buffer + sz;
m_end = buffer + cap;
}
}








share







New contributor




Einiemand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    0














    Here is a simple version of STL vector. I want to check if I implement it correctly but STL source code is not very readable for me. Any suggestion is welcomed!



    namespace nonstd {

    template<typename Ty>
    class vector
    {
    public:
    using iterator = Ty * ;
    using const_iterator = const Ty*;

    vector();
    explicit vector(const size_t count);
    vector(const size_t count, const Ty& val);
    vector(const vector& other);
    vector(vector&& other);
    ~vector();

    vector& operator=(const vector& other);
    vector& operator=(vector&& other);

    size_t size() const;
    size_t capacity() const;

    void push_back(const Ty& val);
    void push_back(Ty&& val);
    void pop_back();

    Ty& front();
    const Ty& front() const;
    Ty& back();
    const Ty& back() const;
    Ty& operator(const size_t pos);
    const Ty& operator(const size_t pos) const;

    iterator begin();
    const_iterator begin() const;
    iterator end();
    const_iterator end() const;
    private:
    Ty * buffer;
    iterator m_first;
    iterator m_last;
    iterator m_end;

    void realloc(const size_t factor, const size_t carry);
    void alloc(const size_t cap);
    };

    template<typename Ty>
    vector<Ty>::vector() : buffer(new Ty[10]), m_first(buffer), m_last(buffer), m_end(buffer + 10) {

    }

    template<typename Ty>
    vector<Ty>::vector(const size_t count) : buffer(new Ty[count]), m_first(buffer), m_last(buffer + count), m_end(buffer + count) {

    }

    template<typename Ty>
    vector<Ty>::vector(const size_t count, const Ty& val) : buffer(new Ty[count]), m_first(buffer), m_last(buffer + count), m_end(buffer + count) {
    while (count--) {
    buffer[count] = val;
    }
    }

    template<typename Ty>
    vector<Ty>::vector(const vector& other) : buffer(new Ty[other.capacity()]), m_first(buffer), m_last(buffer + other.size()), m_end(buffer + other.capacity()) {
    for (size_t i = 0; i < size(); ++i) {
    buffer[i] = other[i];
    }
    }

    template<typename Ty>
    vector<Ty>::vector(vector&& other) : buffer(other.buffer), m_first(other.m_first), m_last(other.m_last), m_end(other.m_end) {
    other.buffer = nullptr;
    other.m_first = other.m_last = other.m_end = nullptr;
    }

    template<typename Ty>
    vector<Ty>::~vector() {
    if (buffer != nullptr) {
    m_first = m_last = m_end = nullptr;
    delete buffer;
    }
    }

    template<typename Ty>
    vector<Ty>& vector<Ty>::operator=(const vector<Ty>& other) {
    if (this == &other) {
    return *this;
    }
    this->~vector();
    buffer = new Ty[other.capacity()];
    m_first = buffer;
    m_last = buffer + other.size();
    m_end = buffer + other.capacity();
    for (size_t i = 0; i < size(); ++i) {
    buffer[i] = other[i];
    }
    return *this;
    }

    template<typename Ty>
    vector<Ty>& vector<Ty>::operator=(vector<Ty>&& other) {
    if (this == &other) {
    return *this;
    }
    this->~vector();

    buffer = other.buffer;
    m_first = other.m_first;
    m_last = other.m_last;
    m_end = other.m_end;

    other.buffer = nullptr;
    other.m_first = other.m_last = other.m_end = nullptr;
    return *this;
    }

    template<typename Ty>
    size_t vector<Ty>::size() const {
    return static_cast<size_t>(m_last - m_first);
    }

    template<typename Ty>
    size_t vector<Ty>::capacity() const {
    return static_cast<size_t>(m_end - m_first);
    }

    template<typename Ty>
    void vector<Ty>::push_back(const Ty& val) {
    if (size() < capacity()) {
    *(m_last++) = val;
    return;
    }
    realloc(2, 2);
    *(m_last++) = val;
    }

    template<typename Ty>
    void vector<Ty>::push_back(Ty&& val) {
    if (size() < capacity()) {
    *(m_last++) = std::move(val);
    return;
    }
    realloc(2, 2);
    *(m_last++) = std::move(val);
    }

    template<typename Ty>
    void vector<Ty>::pop_back() {
    if (size() == 0) {
    throw std::exception("vector is empty");
    }
    (--m_last)->~Ty();
    }

    template<typename Ty>
    Ty& vector<Ty>::front() {
    if (size() == 0) {
    throw std::exception("front(): vector is empty");
    }
    return *begin();
    }

    template<typename Ty>
    const Ty& vector<Ty>::front() const {
    if (size() == 0) {
    throw std::exception("front(): vector is empty");
    }
    return *begin();
    }

    template<typename Ty>
    Ty& vector<Ty>::back() {
    if (size() == 0) {
    throw std::exception("back(): vector is empty");
    }
    return *(end() - 1);
    }

    template<typename Ty>
    const Ty& vector<Ty>::back() const {
    if (size() == 0) {
    throw std::exception("back(): vector is empty");
    }
    return *(end() - 1);
    }

    template<typename Ty>
    Ty& vector<Ty>::operator(const size_t pos) {
    if (pos >= size()) {
    throw std::exception("index out of range");
    }
    return buffer[pos];
    }

    template<typename Ty>
    const Ty& vector<Ty>::operator(const size_t pos) const {
    if (pos >= size()) {
    throw std::exception("index out of range");
    }
    return buffer[pos];
    }

    template<typename Ty>
    typename vector<Ty>::iterator vector<Ty>::begin() {
    return m_first;
    }

    template<typename Ty>
    typename vector<Ty>::iterator vector<Ty>::end() {
    return m_last;
    }

    template<typename Ty>
    typename vector<Ty>::const_iterator vector<Ty>::begin() const {
    return m_first;
    }

    template<typename Ty>
    typename vector<Ty>::const_iterator vector<Ty>::end() const {
    return m_last;
    }

    template<typename Ty>
    void vector<Ty>::realloc(const size_t factor, const size_t carry) {
    alloc(capacity() * factor + carry);
    }

    template<typename Ty>
    void vector<Ty>::alloc(const size_t cap) {
    Ty* new_buffer = new Ty[cap];
    size_t sz = size();
    for (size_t i = 0; i < sz; ++i) {
    new_buffer[i] = buffer[i];
    }
    this->~vector();
    buffer = new_buffer;
    m_first = buffer;
    m_last = buffer + sz;
    m_end = buffer + cap;
    }
    }








    share







    New contributor




    Einiemand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      0












      0








      0







      Here is a simple version of STL vector. I want to check if I implement it correctly but STL source code is not very readable for me. Any suggestion is welcomed!



      namespace nonstd {

      template<typename Ty>
      class vector
      {
      public:
      using iterator = Ty * ;
      using const_iterator = const Ty*;

      vector();
      explicit vector(const size_t count);
      vector(const size_t count, const Ty& val);
      vector(const vector& other);
      vector(vector&& other);
      ~vector();

      vector& operator=(const vector& other);
      vector& operator=(vector&& other);

      size_t size() const;
      size_t capacity() const;

      void push_back(const Ty& val);
      void push_back(Ty&& val);
      void pop_back();

      Ty& front();
      const Ty& front() const;
      Ty& back();
      const Ty& back() const;
      Ty& operator(const size_t pos);
      const Ty& operator(const size_t pos) const;

      iterator begin();
      const_iterator begin() const;
      iterator end();
      const_iterator end() const;
      private:
      Ty * buffer;
      iterator m_first;
      iterator m_last;
      iterator m_end;

      void realloc(const size_t factor, const size_t carry);
      void alloc(const size_t cap);
      };

      template<typename Ty>
      vector<Ty>::vector() : buffer(new Ty[10]), m_first(buffer), m_last(buffer), m_end(buffer + 10) {

      }

      template<typename Ty>
      vector<Ty>::vector(const size_t count) : buffer(new Ty[count]), m_first(buffer), m_last(buffer + count), m_end(buffer + count) {

      }

      template<typename Ty>
      vector<Ty>::vector(const size_t count, const Ty& val) : buffer(new Ty[count]), m_first(buffer), m_last(buffer + count), m_end(buffer + count) {
      while (count--) {
      buffer[count] = val;
      }
      }

      template<typename Ty>
      vector<Ty>::vector(const vector& other) : buffer(new Ty[other.capacity()]), m_first(buffer), m_last(buffer + other.size()), m_end(buffer + other.capacity()) {
      for (size_t i = 0; i < size(); ++i) {
      buffer[i] = other[i];
      }
      }

      template<typename Ty>
      vector<Ty>::vector(vector&& other) : buffer(other.buffer), m_first(other.m_first), m_last(other.m_last), m_end(other.m_end) {
      other.buffer = nullptr;
      other.m_first = other.m_last = other.m_end = nullptr;
      }

      template<typename Ty>
      vector<Ty>::~vector() {
      if (buffer != nullptr) {
      m_first = m_last = m_end = nullptr;
      delete buffer;
      }
      }

      template<typename Ty>
      vector<Ty>& vector<Ty>::operator=(const vector<Ty>& other) {
      if (this == &other) {
      return *this;
      }
      this->~vector();
      buffer = new Ty[other.capacity()];
      m_first = buffer;
      m_last = buffer + other.size();
      m_end = buffer + other.capacity();
      for (size_t i = 0; i < size(); ++i) {
      buffer[i] = other[i];
      }
      return *this;
      }

      template<typename Ty>
      vector<Ty>& vector<Ty>::operator=(vector<Ty>&& other) {
      if (this == &other) {
      return *this;
      }
      this->~vector();

      buffer = other.buffer;
      m_first = other.m_first;
      m_last = other.m_last;
      m_end = other.m_end;

      other.buffer = nullptr;
      other.m_first = other.m_last = other.m_end = nullptr;
      return *this;
      }

      template<typename Ty>
      size_t vector<Ty>::size() const {
      return static_cast<size_t>(m_last - m_first);
      }

      template<typename Ty>
      size_t vector<Ty>::capacity() const {
      return static_cast<size_t>(m_end - m_first);
      }

      template<typename Ty>
      void vector<Ty>::push_back(const Ty& val) {
      if (size() < capacity()) {
      *(m_last++) = val;
      return;
      }
      realloc(2, 2);
      *(m_last++) = val;
      }

      template<typename Ty>
      void vector<Ty>::push_back(Ty&& val) {
      if (size() < capacity()) {
      *(m_last++) = std::move(val);
      return;
      }
      realloc(2, 2);
      *(m_last++) = std::move(val);
      }

      template<typename Ty>
      void vector<Ty>::pop_back() {
      if (size() == 0) {
      throw std::exception("vector is empty");
      }
      (--m_last)->~Ty();
      }

      template<typename Ty>
      Ty& vector<Ty>::front() {
      if (size() == 0) {
      throw std::exception("front(): vector is empty");
      }
      return *begin();
      }

      template<typename Ty>
      const Ty& vector<Ty>::front() const {
      if (size() == 0) {
      throw std::exception("front(): vector is empty");
      }
      return *begin();
      }

      template<typename Ty>
      Ty& vector<Ty>::back() {
      if (size() == 0) {
      throw std::exception("back(): vector is empty");
      }
      return *(end() - 1);
      }

      template<typename Ty>
      const Ty& vector<Ty>::back() const {
      if (size() == 0) {
      throw std::exception("back(): vector is empty");
      }
      return *(end() - 1);
      }

      template<typename Ty>
      Ty& vector<Ty>::operator(const size_t pos) {
      if (pos >= size()) {
      throw std::exception("index out of range");
      }
      return buffer[pos];
      }

      template<typename Ty>
      const Ty& vector<Ty>::operator(const size_t pos) const {
      if (pos >= size()) {
      throw std::exception("index out of range");
      }
      return buffer[pos];
      }

      template<typename Ty>
      typename vector<Ty>::iterator vector<Ty>::begin() {
      return m_first;
      }

      template<typename Ty>
      typename vector<Ty>::iterator vector<Ty>::end() {
      return m_last;
      }

      template<typename Ty>
      typename vector<Ty>::const_iterator vector<Ty>::begin() const {
      return m_first;
      }

      template<typename Ty>
      typename vector<Ty>::const_iterator vector<Ty>::end() const {
      return m_last;
      }

      template<typename Ty>
      void vector<Ty>::realloc(const size_t factor, const size_t carry) {
      alloc(capacity() * factor + carry);
      }

      template<typename Ty>
      void vector<Ty>::alloc(const size_t cap) {
      Ty* new_buffer = new Ty[cap];
      size_t sz = size();
      for (size_t i = 0; i < sz; ++i) {
      new_buffer[i] = buffer[i];
      }
      this->~vector();
      buffer = new_buffer;
      m_first = buffer;
      m_last = buffer + sz;
      m_end = buffer + cap;
      }
      }








      share







      New contributor




      Einiemand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      Here is a simple version of STL vector. I want to check if I implement it correctly but STL source code is not very readable for me. Any suggestion is welcomed!



      namespace nonstd {

      template<typename Ty>
      class vector
      {
      public:
      using iterator = Ty * ;
      using const_iterator = const Ty*;

      vector();
      explicit vector(const size_t count);
      vector(const size_t count, const Ty& val);
      vector(const vector& other);
      vector(vector&& other);
      ~vector();

      vector& operator=(const vector& other);
      vector& operator=(vector&& other);

      size_t size() const;
      size_t capacity() const;

      void push_back(const Ty& val);
      void push_back(Ty&& val);
      void pop_back();

      Ty& front();
      const Ty& front() const;
      Ty& back();
      const Ty& back() const;
      Ty& operator(const size_t pos);
      const Ty& operator(const size_t pos) const;

      iterator begin();
      const_iterator begin() const;
      iterator end();
      const_iterator end() const;
      private:
      Ty * buffer;
      iterator m_first;
      iterator m_last;
      iterator m_end;

      void realloc(const size_t factor, const size_t carry);
      void alloc(const size_t cap);
      };

      template<typename Ty>
      vector<Ty>::vector() : buffer(new Ty[10]), m_first(buffer), m_last(buffer), m_end(buffer + 10) {

      }

      template<typename Ty>
      vector<Ty>::vector(const size_t count) : buffer(new Ty[count]), m_first(buffer), m_last(buffer + count), m_end(buffer + count) {

      }

      template<typename Ty>
      vector<Ty>::vector(const size_t count, const Ty& val) : buffer(new Ty[count]), m_first(buffer), m_last(buffer + count), m_end(buffer + count) {
      while (count--) {
      buffer[count] = val;
      }
      }

      template<typename Ty>
      vector<Ty>::vector(const vector& other) : buffer(new Ty[other.capacity()]), m_first(buffer), m_last(buffer + other.size()), m_end(buffer + other.capacity()) {
      for (size_t i = 0; i < size(); ++i) {
      buffer[i] = other[i];
      }
      }

      template<typename Ty>
      vector<Ty>::vector(vector&& other) : buffer(other.buffer), m_first(other.m_first), m_last(other.m_last), m_end(other.m_end) {
      other.buffer = nullptr;
      other.m_first = other.m_last = other.m_end = nullptr;
      }

      template<typename Ty>
      vector<Ty>::~vector() {
      if (buffer != nullptr) {
      m_first = m_last = m_end = nullptr;
      delete buffer;
      }
      }

      template<typename Ty>
      vector<Ty>& vector<Ty>::operator=(const vector<Ty>& other) {
      if (this == &other) {
      return *this;
      }
      this->~vector();
      buffer = new Ty[other.capacity()];
      m_first = buffer;
      m_last = buffer + other.size();
      m_end = buffer + other.capacity();
      for (size_t i = 0; i < size(); ++i) {
      buffer[i] = other[i];
      }
      return *this;
      }

      template<typename Ty>
      vector<Ty>& vector<Ty>::operator=(vector<Ty>&& other) {
      if (this == &other) {
      return *this;
      }
      this->~vector();

      buffer = other.buffer;
      m_first = other.m_first;
      m_last = other.m_last;
      m_end = other.m_end;

      other.buffer = nullptr;
      other.m_first = other.m_last = other.m_end = nullptr;
      return *this;
      }

      template<typename Ty>
      size_t vector<Ty>::size() const {
      return static_cast<size_t>(m_last - m_first);
      }

      template<typename Ty>
      size_t vector<Ty>::capacity() const {
      return static_cast<size_t>(m_end - m_first);
      }

      template<typename Ty>
      void vector<Ty>::push_back(const Ty& val) {
      if (size() < capacity()) {
      *(m_last++) = val;
      return;
      }
      realloc(2, 2);
      *(m_last++) = val;
      }

      template<typename Ty>
      void vector<Ty>::push_back(Ty&& val) {
      if (size() < capacity()) {
      *(m_last++) = std::move(val);
      return;
      }
      realloc(2, 2);
      *(m_last++) = std::move(val);
      }

      template<typename Ty>
      void vector<Ty>::pop_back() {
      if (size() == 0) {
      throw std::exception("vector is empty");
      }
      (--m_last)->~Ty();
      }

      template<typename Ty>
      Ty& vector<Ty>::front() {
      if (size() == 0) {
      throw std::exception("front(): vector is empty");
      }
      return *begin();
      }

      template<typename Ty>
      const Ty& vector<Ty>::front() const {
      if (size() == 0) {
      throw std::exception("front(): vector is empty");
      }
      return *begin();
      }

      template<typename Ty>
      Ty& vector<Ty>::back() {
      if (size() == 0) {
      throw std::exception("back(): vector is empty");
      }
      return *(end() - 1);
      }

      template<typename Ty>
      const Ty& vector<Ty>::back() const {
      if (size() == 0) {
      throw std::exception("back(): vector is empty");
      }
      return *(end() - 1);
      }

      template<typename Ty>
      Ty& vector<Ty>::operator(const size_t pos) {
      if (pos >= size()) {
      throw std::exception("index out of range");
      }
      return buffer[pos];
      }

      template<typename Ty>
      const Ty& vector<Ty>::operator(const size_t pos) const {
      if (pos >= size()) {
      throw std::exception("index out of range");
      }
      return buffer[pos];
      }

      template<typename Ty>
      typename vector<Ty>::iterator vector<Ty>::begin() {
      return m_first;
      }

      template<typename Ty>
      typename vector<Ty>::iterator vector<Ty>::end() {
      return m_last;
      }

      template<typename Ty>
      typename vector<Ty>::const_iterator vector<Ty>::begin() const {
      return m_first;
      }

      template<typename Ty>
      typename vector<Ty>::const_iterator vector<Ty>::end() const {
      return m_last;
      }

      template<typename Ty>
      void vector<Ty>::realloc(const size_t factor, const size_t carry) {
      alloc(capacity() * factor + carry);
      }

      template<typename Ty>
      void vector<Ty>::alloc(const size_t cap) {
      Ty* new_buffer = new Ty[cap];
      size_t sz = size();
      for (size_t i = 0; i < sz; ++i) {
      new_buffer[i] = buffer[i];
      }
      this->~vector();
      buffer = new_buffer;
      m_first = buffer;
      m_last = buffer + sz;
      m_end = buffer + cap;
      }
      }






      c++ vectors





      share







      New contributor




      Einiemand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      Einiemand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      Einiemand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 6 mins ago









      EiniemandEiniemand

      1




      1




      New contributor




      Einiemand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Einiemand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Einiemand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Einiemand is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211241%2fsimple-c-stl-vector-implementationneed-criticism%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Einiemand is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Einiemand is a new contributor. Be nice, and check out our Code of Conduct.













          Einiemand is a new contributor. Be nice, and check out our Code of Conduct.












          Einiemand is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211241%2fsimple-c-stl-vector-implementationneed-criticism%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'