#include
#include
#include
#include
using namespace std;
/*
max_size();返回容器最大的可以存储的元素
pop_back()
*/
template//allocator
class Meng
{
private:
int m_size;
int m_capacity;
T * objects;
public:
Meng()
{
m_size = 0;
m_capacity = 0;
objects = NULL;
}
Meng(int count)
{
m_size = count;
m_capacity = count;
objects = new T[m_capacity];
for(int i = 0; i < count; i++)
objects[i] = 0;
}
Meng(int count, T element)
{
m_size = count;
m_capacity = count;
objects = new T[m_capacity];
for(int i = 0; i < count; i++)
objects[i] = element;
}
Meng(Meng &obj)
{
m_size = obj.m_size;
m_capacity = obj.m_capacity;
objects = new T[m_capacity];
//strcpy(objects, obj.objects);
for(int i = 0; i < m_size; i++)
objects[i] = obj.objects[i];
}
~Meng()
{
if(objects != NULL)
delete []objects;
objects = NULL;
}
int size()
{
return m_size;
}
int capacity()
{
return m_capacity;
}
bool empty()
{
if(m_size == 0)
return true;
else
return false;
}
void push_back(T value)
{
if(m_capacity == 0)
{
m_capacity++;
objects = new T[m_capacity];
}
if(m_size < m_capacity)
{
objects[m_size] = value;
m_size++;
}
else
{
T* tmp = new T[m_capacity];
for(int i = 0; i < m_capacity; i++)
{
tmp[i] = objects[i];
}
delete []objects;
m_capacity *= 2;
objects = new T[m_capacity];
for(int i = 0; i < m_size; i++)
{
objects[i] = tmp[i];
}
delete []tmp;
objects[m_size] = value;
m_size++;
}
}
void pop_back()
{
m_size--;
}
void clear()
{
m_size = 0;
}
T& at(int pos)
{
return objects[pos];
}
T& front()
{
return objects[0];
}
T& back()
{
return objects[m_size];
}
void assign(int n, const T& x)
{
//T* tmp = new T[n];
if(n <= m_capacity)
{
for(int i = 0; i < n; i++)
objects[i] = x;
m_size = n;
}
else
{
if(objects != NULL)
delete []objects;
objects = new T[n];
for(int i = 0; i < n; i++)
objects[i] = x;
m_size = n;
m_capacity = n;
}
//delete []tmp;
}
Meng& operator = (Meng& other)
{
if(this != other) //注意传入参数是自身的情况
{
m_size = other.m_size;
m_capacity = other.m_capacity;
delete []objects;
if(other.objects != NULL)
{
objects = new T[m_capacity];
for(int i = 0; i < m_size; i++)
objects[i] = other.objects[i];
}
else
objects = NULL;
}
return this;
}
T& operator[](int pos) //注意不是Meng
{
return objects[pos];
}
class iterator
{
T* iter;
public:
iterator()
{
iter = NULL;
}
iterator(T* other)
{
iter = other;
}
int operator - (T* num)
{
return iter - num;
}
T operator *()
{
return *iter;
}
T*& operator ++(int)//如果不是引用影响也不大吧?
{
iter++;
return iter;
}
T* operator + (int other)
{
return iter+other;
}
bool operator != (iterator& other)
{
return iter != other.iter;
}
bool operator == (iterator& other)
{
return iter == other.iter;
}
iterator& operator = (iterator& other)
{
iter = other.iter;
return this;
}
iterator& operator = (T* other)
{
iter = other;
return this;
}
};
T* begin() //Meng::iterator& begin()
{
//iterator tmp = &objects[0];
return &objects[0];
}
T* end()
{
//iterator tmp = &objects[m_size]; //注意不是m_size-1
return &objects[m_size];
}
iterator insert(iterator it, const T& x)
{
//查找迭代器指向元素的位置
int location = 0;
/*for(int i = 0; i < m_size; i++) //?为什么这种不行
{
if(objects[i] == (*it))
{
location = i;
break;
}
}*/
location = it - objects;
if(m_capacity == 0)//如何解决插入begin() + n的问题
{
m_capacity++;
objects = new T[m_capacity];
}
if(m_size < m_capacity)
{
for(int j = m_size-1; j >= location; j--)
{
objects[j+1] = objects[j];
}
objects[location] = x;
//(*it) = x;
m_size++;
}
else
{
T* tmp = new T[m_capacity];
for(int i = 0; i < m_capacity; i++)
{
tmp[i] = objects[i];
}
delete []objects;
m_capacity *= 2;
objects = new T[m_capacity];
for(int i = 0; i < m_size; i++)
{
objects[i] = tmp[i];
}
delete []tmp;
for(int j = m_size-1; j >= location; j--)
{
objects[j+1] = objects[j];
}
objects[location] = x;
//*it = x;
m_size++;
}
iterator tmp = &objects[location];
return tmp;//为啥不能直接return it
}
void insert(iterator it, int n, const T& x)
{
int location = it - objects;
if(m_size+n > m_capacity)
reserve(m_size+n);//顺序?可以放在reserve函数前?
for(int j = m_size-1; j >= location; j--)
{
objects[j+n] = objects[j];
}
for(int i = location; i < location+n; i++)
objects[i] = x;
m_size += n;
}
void reserve(int new_capacity)
{
if(new_capacity > m_capacity)
{
T* tmp = new T[m_size];
for(int i = 0; i < m_size; i++)
{
tmp[i] = objects[i];
}
delete []objects;
objects = new T[new_capacity];
for(int i = 0; i < m_size; i++)
objects[i] = tmp[i];
delete []tmp;
for(int i = m_size; i < new_capacity; i++)
objects[i] = 0;
m_capacity = new_capacity;
}
}
void resize(int new_size, const T value)
{
if(new_size < m_size)
{
m_size = new_size;
}else if(new_size > m_size)
{
if(new_size <= m_capacity)
{
for(int i = m_size; i < new_size; i++)
objects[i] = value;
}else
{
reserve(new_size);
for(int i = m_size; i < new_size; i++)
objects[i] = value;
}
m_size = new_size;
}
}
void resize(int new_size)
{
resize(new_size, 0);
}
};
void show(int n)
{
cout << n << '\t';
}
int main()
{
Meng v(5, 2);
/*v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);*/
/*cout << v.size() << '\t' << v.capacity() << endl;
Meng ::iterator it = v.begin()+1;
Meng ::iterator it2 = v.insert(it , 10);
cout << v.size() << '\t' << v.capacity() << endl;
cout << *(it2+1) <