In the previous article, we talked about list. In this article, let's discuss vector
Both vector and list are common containers in the STL.
It is a sequential container that supports random access.
And numbers are very similar, are a continuous memory space. Arrays have functions, which can basically be achieved.
The difference from the array is that the space allocated by the array is dead and immutable; while the vector can continue to insert elements, and it will increase its capacity. When the programmer can't know how much space is needed, this is undoubtedly the best method, and can save space to the greatest extent.
Code directly below.
The notes are clear and hopefully helpful to readers.
#include<iostream> #include<assert.h> #include<windows.h> using namespace std; typedef int DataType; class Vector { public: Vector(size_t capacity = 3) :_data(new DataType[capacity]) //Open up space , _size(0) , _capacity(capacity) {} //There are n elements whose value is data Vector(const DataType& data, size_t n) {} //copy constructor Vector(const Vector& v) //:_capacity(v._capacity) //_size(v._size) { _data = new DataType(_capacity); // Compare the pros and cons of 1 and 2 //1. //memcpy(_data, v._data, sizeof(DataType)* _size); //2. for (size_t i = 0; i < _size; i++) _data[i] = v._data[i]; } //copy operator overloading Vector& operator=(const Vector& v) { if (this != &v) { DataType* temp = new DataType[v._capacity]; for (size_t i = 0; i < v._size; i++) { temp[i] = v._data[i]; } delete _data; _data = temp; _capacity = v._capacity; _size = v._size; } return *this; } //destructor ~Vector() { if (_data != NULL) { delete _data; _data = NULL; _size = 0; _capacity = 0; } } void PrintVector(); void PushBack(const DataType& data); void PopBack(); void Insert(size_t pos, const DataType& data); void Erase(size_t pos); void Find(const DataType& data); void Clear(); size_t Size(); size_t Capacity(); void ReSize(size_t size, const DataType& data); bool Empty(); void Front(); void Back(); DataType& operator[](size_t index); DataType& At(size_t n); private: //check if capacity is full void CheckCapacity() { //Actual size > capacity, expansion if (_size >= _capacity) { DataType* temp = new DataType[_capacity * 2 + 3]; for (int i = 0; i < _size; i++) { temp[i] = _data[i]; } delete _data; _data = temp; //_capacity *= 2; } } DataType* _data; size_t _size; size_t _capacity; };
The following is the function implementation part
#include"Vector.h" void Vector::PrintVector() { for (size_t i = 0; i < _size; i++) cout << _data[i]; cout << endl; } void Vector::PushBack(const DataType& data) { CheckCapacity(); _data[_size++] = data; } bool Vector::Empty() { return _size == 0; } void Vector::PopBack() { if (!Empty()) --_size; } //Insert anywhere void Vector::Insert(size_t pos, const DataType& data) { //check if pos is valid assert(pos); //check capacity CheckCapacity(); for (int i = pos; i < _size; i++) { //assign the previous one to the next one _data[i] = _data[i-1]; } _data[pos - 1] = data; //Assign data to the posth element _size++; } //delete anywhere void Vector::Erase(size_t pos) { assert(pos); if (!Empty()) { //The element before pos does not need to be changed, just move the following elements collectively forward one, and finally give size++ for (int i = pos; i < _size; i++) { _data[i - 1] = _data[i]; } _size--; } } void Vector::Find(const DataType& data) { for (int i = 0; i < _size; i++) { if (_data[i] == data) cout << "found" << endl; } } // clear the element void Vector::Clear() { if (_data != NULL) { delete _data; _data = NULL; _capacity = 0; _size = 0; } } size_t Vector::Size() { return _size; } size_t Vector::Capacity() { return _capacity; } //Change the size of _size in Vector void Vector::ReSize(size_t size, const DataType& data) { //size<_size if (size < _size) _size = size; // size > _size else if (size>_size) { int _oldsize = _size; _size = size; while (1) { //_size < size < _capacity if (size < _capacity) break; else CheckCapacity(); } //size > _capacity for (int i = _oldsize; i < _size; i++) { _data[i] = data; } } } void Vector::Front() { cout<<"the first is:"<< _data[0]<<endl; } void Vector::Back() { cout << "the last is:" << _data[_size-1] << endl; } //DataType& operator[](size_t index) DataType& Vector::At(size_t n) { assert(n > 0 && n <= _size); return _data[n - 1]; } void FunTest() { Vector v; v.PushBack(1); v.PushBack(2); v.PushBack(3); v.PushBack(4); v.PushBack(5); v.PushBack(6); v.PrintVector (); v.PopBack(); v.PrintVector (); //v.Insert(2,6); //v.PrintVector(); //Can be inserted, but will destroy the following number, and will access out of bounds v.Erase(5); v.PrintVector (); v.Find (2); v.Front(); v.Back(); } intmain() { FunTest (); system("pause"); return 0; }
Next are the test results: