Skip to content
On this page

MyVector.cpp


标签:代码片段CPP/basic  

使用 template 实现 <Vector>

cpp
#include <cstring>
#include <string>

template <typename T>
class MyVector {
 private:
  T* first;
  T* last;
  T* end;

 public:
  MyVector();
  MyVector(long long size);
  MyVector(const MyVector& v);
  ~MyVector();
  T& at(long long pos);
  T& front();
  T& back();
  bool full();
  bool empty();
  void clear();
  void expand();
  void pop_back();
  long long size();
  long long capacity();
  void push_back(const T& v);
};

template <typename T>
MyVector<T>::MyVector() {
  first = new T[1];
  last = first;
  end = first + 1;
}

template <typename T>
MyVector<T>::MyVector(long long size) {
  first = new T[size];
  last = first;
  end = first + size;
}

template <typename T>
MyVector<T>::MyVector(const MyVector& v) {
  first = new T[v.size()];
  last = first + v.size();
  end = first + v.size();
  for (int i = 0; i < v.size(); i++) {
    first[i] = v.first[i];
  }
}

template <typename T>
MyVector<T>::~MyVector<T>() {
  delete[] first;
}

template <typename T>
T& MyVector<T>::at(long long pos) {
  if (pos < 0 || pos >= this->size())
    throw std::string("Out of range");
  return first[pos];
}

template <typename T>
bool MyVector<T>::empty() {
  return first == last;
}

template <typename T>
bool MyVector<T>::full() {
  return last == end;
}

template <typename T>
T& MyVector<T>::front() {
  return *first;
}

template <typename T>
T& MyVector<T>::back() {
  return *(last - 1);
}

template <typename T>
long long MyVector<T>::size() {
  return last - first;
}

template <typename T>
void MyVector<T>::clear() {
  last = first;
  end = first + 1;
}

template <typename T>
void MyVector<T>::expand() {
  long long size = this->size();
  T* new_first = new T[size * 2];
  memccpy(new_first, first, sizeof(T), size);
  delete[] first;
  first = new_first;
  last = first + size;
  end = first + size * 2;
}

template <typename T>
void MyVector<T>::pop_back() {
  last--;
}

template <typename T>
void MyVector<T>::push_back(const T& v) {
  if (this->full())
    this->expand();
  *last = v;
  last++;
}

template <typename T>
long long MyVector<T>::capacity() {
  return end - first;
}
cpp
#include <iostream>
#include "MyVector.hpp"

using namespace std;

int main() {
  MyVector<int> v1;
  cout << boolalpha;
  cout << v1.empty() << endl;
  cout << v1.size() << endl;
  cout << v1.capacity() << endl;
  cout << v1.full() << endl;
  cout << "-------------------" << endl;
  v1.push_back(10);
  cout << v1.empty() << endl;
  cout << v1.size() << endl;
  cout << v1.capacity() << endl;
  cout << v1.full() << endl;
  cout << "-------------------" << endl;
  v1.push_back(20);
  cout << v1.empty() << endl;
  cout << v1.size() << endl;
  cout << v1.capacity() << endl;
  cout << v1.full() << endl;
  cout << "-------------------" << endl;
  cout << v1.at(1) << endl;
  cout << "-------------------" << endl;
  try {
    cout << v1.at(2) << endl;
  } catch (string s) {
    cout << s << endl;
  }
  cout << "-------------------" << endl;
  v1.at(0) = 30;
  v1.at(v1.size() - 1) = 40;
  cout << v1.front() << endl;
  cout << v1.back() << endl;
  cout << "-------------------" << endl;
  v1.pop_back();
  cout << v1.empty() << endl;
  cout << v1.size() << endl;
  cout << v1.capacity() << endl;
  cout << v1.full() << endl;
  cout << "-------------------" << endl;
  v1.clear();
  cout << v1.empty() << endl;
  cout << v1.size() << endl;
  cout << v1.capacity() << endl;
  cout << v1.full() << endl;
  cout << "-------------------" << endl;
  for(int i = 0; i < 20; i++) {
    v1.push_back(i);
  }
  cout << v1.empty() << endl;
  cout << v1.size() << endl;
  cout << v1.capacity() << endl;
  cout << v1.full() << endl;
}

Last updated: