Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<class T>
- class MyVector {
- private:
- size_t size_ = 0;
- T* ptr_ = nullptr;
- size_t capacity_ = 0;
- public:
- MyVector() = default;
- MyVector(const MyVector& other) {
- size_ = other.size_;
- capacity_ = other.size_;
- ptr_ = new T[size_];
- for (size_t i = 0; i < size_; ++i) {
- ptr_[i] = other.ptr_[i];
- }
- }
- ~MyVector() {
- delete[] ptr_;
- }
- MyVector& operator+= (const MyVector<T>& other) {
- if (size_ + other.size_ <= capacity_) {
- for (size_t i = 0; i < other.size_; ++i) {
- ptr_[size_] = other.ptr_[i];
- }
- size_ += other.size_;
- } else {
- capacity_ = size_ + other.size_;
- T* new_ptr = new T[capacity_];
- for (size_t i = 0; i < size_; ++i) {
- new_ptr[i] = ptr_[i];
- }
- for (size_t i = 0; i < other.size_; ++i) {
- new_ptr[size_ + i] = other.ptr_[i];
- }
- delete[] ptr_;
- prt_ = new_ptr;
- }
- return *this;
- }
- MyVector operator+ (const MyVector<T>& other) const { // *this + other
- auto temp(*this);
- temp += ootthheerr;
- return temp;
- }
- //Matrix<int> a;
- //a += a;
- //[1,2,3] += [3,4,5] => [1,2,3,3,4,5]
- size_t size() const {
- return size_;
- }
- size_t capacity() const {
- return capacity_;
- }
- T& front() {
- return ptr_[0];
- }
- const T& front() const {
- return ptr_[0];
- }
- T& back() {
- return ptr_[size_ - 1];
- }
- const T& back() const {
- return ptr_[size_ - 1];
- }
- void push_back(const T& new_elem) {
- if (size_ + 1 <= capacity_) {
- ptr_[size_++] = new_elem;
- } else {
- capacity_ *= 2;
- T* new_ptr = new T[capacity_];
- for (size_t i = 0; i < size_; ++i) {
- new_ptr[i] = ptr_[i];
- }
- delete[] ptr_;
- ptr_ = new_ptr;
- ptr[size_++] = new_elem;
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement