myVector
#pragma once
#include<iostream>
#include<cstring>
using namespace std;
template<typename T>
class myVector
{
public:
myVector();
myVector(int len, T element);
myVector(const myVector<T>& rhc);
~myVector();
myVector<T>& operator=(const myVector<T>& rhc);
T& operator[](int index);
void push_back(T element);
void pop_back();
void insert(int pos, T element);
void clear();
template<typename T2>
friend ostream& operator<<(ostream& out, const myVector<T2>& rhc);
private:
T* m_element;
int m_size;
int m_capacity;
};
template<typename T>
myVector<T>::~myVector()
{
delete[] m_element;
}
template<typename T>
myVector<T>::myVector() :m_capacity(16), m_size(0)
{
this->m_element = new int[m_capacity];
}
template<typename T>
myVector<T>::myVector(int len, T element) :m_capacity(16)
{
m_capacity = m_capacity + len;
m_element = new int[m_capacity];
m_size = len;
for (int i = 0; i < len; i++)
memcpy(&m_element[i], &element, sizeof(T));
}
template<typename T>
myVector<T>::myVector(const myVector<T>& rhs)
{
m_capacity = rhs.m_capacity;
m_size = rhs.m_size;
m_element = new int[m_capacity];
memcpy(m_element, rhs.m_element, sizeof(T) * m_size);
}
template<typename T>
myVector<T>& myVector<T>::operator=(const myVector<T>& rhs)
{
if (m_element != nullptr)
{
delete[] m_element;
m_element = nullptr;
}
m_capacity = rhs.m_capacity;
m_size = rhs.m_size;
m_element = new int[m_capacity];
memcpy(m_element, rhs.m_element, sizeof(T) * m_size);
return *this;
}
template<typename T>
T& myVector<T>::operator[](int index)
{
return m_element[index];
}
template<typename T>
void myVector<T>::push_back(T element)
{
if (m_element == nullptr)
{
m_capacity = 16;
m_size = 0;
m_element = new int[m_capacity];
}
if (m_size == m_capacity)
{
m_capacity = m_capacity * 2 + 1;
T* newelement = new int[m_capacity];
memcpy(newelement, m_element, sizeof(T) * m_size);
delete[] m_element;
m_element = newelement;
}
m_element[m_size] = element;
m_size++;
}
template<typename T>
void myVector<T>::pop_back()
{
if (m_size == 0)
{
cout << "error empty!" << endl;
}
else
{
m_element[m_size - 1] = 0;
m_size--;
}
return;
}
template<typename T>
void myVector<T>::clear()
{
if (m_element == nullptr)
return;
else
{
m_capacity = 16;
m_size = 0;
delete[] m_element;
m_element = nullptr;
}
}
template<typename T>
void myVector<T>::insert(int pos, T element)
{
if (m_element == nullptr)
{
m_capacity = 16;
m_element = new int[m_capacity];
m_element[0] = element;
m_size = 1;
}
else
{
if (m_size == m_capacity)
{
m_capacity = m_capacity * 2 + 1;
T* newelement = new int[m_capacity];
memcpy(newelement, m_element, sizeof(T) * m_size);
delete[] m_element;
m_element = newelement;
}
for (int i = m_size; i > pos; i--)
{
m_element[i] = m_element[i - 1];
}
m_element[pos] = element;
m_size++;
}
}
template<typename T2>
std::ostream& operator<<(std::ostream& out, const myVector<T2>& vec)
{
if (vec.m_element == nullptr)
out << "no element!" << endl;
else
{
for (int i = 0; i < vec.m_size; i++)
out << vec.m_element[i] << "\t";
out << std::endl;
}
return out;
}
测试代码
#include<iostream>
#include<vector>
#include"myVector.h"
using namespace std;
int main()
{
myVector<int> my1(10, 5);
my1.insert(0, 1);
cout << my1;
my1.push_back(12);
my1.push_back(10);
my1.pop_back();
cout << my1;
my1.clear();
my1.push_back(12);
cout << my1;
}
测试结果: