这个代码桥段是由自己开发
各位大佬自取
#ifndef MYVECTOR_H
#define MYVECTOR_H
#include<iostream>
using namespace std;
template <typename T>
class MyVector {
T m_container[1000] = { }; //存放数据的数组,初始化为0
T* m_end = m_container; // 指向vector尾部元素的指针,初始化时指向数组第一个元素
public:
// 构造函数
MyVector() = default; // 默认构造函数
MyVector(const initializer_list<T> &list) // 带参数构造函数
{
T* p = m_container;
for (auto i =list.begin() ; i != list.end(); i++)
{
*p = *i;
p++;
}
m_end = p;
}
MyVector(const MyVector& rhs) {//复制构造函数
T* p = m_container;
for (auto i = &rhs.m_container[0]; i != rhs.m_end; i++) {
*p= *i;
p++;
}
m_end = p;
}
~MyVector() {}//析构函数
int size(); //获取元素个数
T& at(int); // 元素访问函数at
T* front(); // 获取首元素地址
T* back(); //获取尾元素地址
void push_back(const T&); // 尾插函数
void pop_back(); // 尾部删除函数
void insert(const T&, int); // 指定位置插入元素
void erase(int); // 删除指定位置元素
void clear(); // 清空所有元素
T& operator [](int); // 重载下标运算符[]
MyVector& operator =(const MyVector&); // 重载赋值运算符
void sort(); // 排序,默认冒泡排序
int binary_search(const T&); // 二分查找算法
friend ostream& operator<< <>(ostream& os, const MyVector& rhs);
MyVector operator +(const MyVector& s);
private:
void swap(int, int);
};
template <typename T>
int MyVector<T>::size() {
return m_end-m_container;
}
template<typename T>
T& MyVector<T>::at(int a) {
return m_container[a];
}
template <typename T>
T* MyVector<T>::front() {
return m_container;
}
template <typename T>
T* MyVector<T>::back() {
return m_end;
}
template<typename T>
void MyVector<T>::push_back(const T& a) {
int x = size();
m_container[x] = a;
}
template<typename T>
void MyVector<T>::pop_back() {
*m_end = {};
m_end--;
}
template<typename T>
void MyVector<T>::insert(const T& a, int b) {
for (T* position = m_end+1; position != m_container + b; position--)
{
*position = *(position - 1);
}
*(m_container + b) = a;
m_end++;
}
template<typename T>
void MyVector<T>::erase(int b) {
for (T* position = m_container+b; position!=m_end ; position++)
{
*position = *(position + 1);
}
*m_end = {};
m_end--;
}
template<typename T>
void MyVector<T>::clear() {
for (auto i = m_container; i<m_end; i++)
{
*i = {};
}
m_end = m_container;
}
template <typename T>
T& MyVector<T>::operator [](int p)
{
// 下标越界警告
if (m_container + p > m_end)
{
cout << "错误:越界!" << endl;
}
return *(m_container + p);
}
template<typename T>
MyVector<T>& MyVector<T>::operator =(const MyVector&rhs) {
if (&rhs == this) return *this; // 不会给自己赋值
T* p1 = m_container; // 指向数组第一个元素的指针
// 遍历
for (const T* p2 = &(rhs.m_container[0]); p2<= rhs.m_end; p2++)
{
*p1 = *p2; // 对应位置元素复制
p1++; // 指针位置+1
}
m_end = p1; // 储存尾部位置
return *this;
}
template <typename T>
ostream& operator<< <>(ostream& os, const MyVector<T>& rhs) {
os << "{";
for (auto it = rhs.m_container; it <= rhs.m_end; it++)
{
os << *it << ",";
}
os << "}";
return os;
}
template<typename T>
void MyVector<T>::sort() {
for (int i = size() - 1; i >= 0; --i)
{
for (int j = 0; j <= i - 1; ++j)
{
if (m_container[j + 1] < m_container[j]) // 如果大于后一个元素
{
swap(j, j + 1); // 两元素交换
}
}
}
}
template<typename T>
void MyVector<T>::swap(int a, int b) {
T temple = m_container[a];
m_container[a] = m_container[b];
m_container[b] = temple;
}
template <typename T>
int MyVector<T>::binary_search(const T& elem)
{
int left = 0; // 左指针
int right = size() - 1; // 右指针
while (left <= right)
{
int middle = (left + right) / 2; // 计算中点位置
if (m_container[middle] == elem)
return middle;
else if (m_container[middle] > elem)
right = middle - 1; // 修改right指针
else
left = middle + 1; // 修改left指针
}
return -1; //查找失败
}
template<typename T>
MyVector<T> MyVector<T>::operator+(const MyVector& s)
{
for (auto it = s.m_container; it != s.m_end; it++)
{
this->push_back(*it);
}
return *this;
}
#endif