c++完成vector函数功能
#include <iostream>
using namespace std;
template <typename T>
class myVecto
{
private:
T *first;
T *last;
T *end;
public:
myVecto():first(nullptr), last(nullptr), end(nullptr) {}
myVecto(int num, const T& val):first(new T[num])
{
for(int i=0; i<num; i++)
{
first[i] = val;
}
last = end = first+num;
}
~myVecto()
{
delete []first;
}
myVecto(const myVecto &other):first(new T[other.end-other.first])
{
last = first+(other.last - other.first);
end = first + (other.end - other.first);
memcpy(first, other.first, (last-first)*sizeof (T));
}
myVecto& operator=(const myVecto& other)
{
delete [] first;
first = new T[other.last - other.first];
last = first + (other.last - other.first);
end = first + (other.end - other.first);
memcpy(first, other.first, (last-first)*sizeof (T));
return *this;
}
T& at(int sub)
{
if(sub<0 || sub>last-first)
{
cout << "段错误" << endl;
}
return first[sub];
}
bool empty()
{
return last == first ? true:false;
}
bool full()
{
return last == end ? true:false;
}
T& front()
{
return *first;
}
T& back()
{
return *(end-1);
}
int size()
{
return last-first;
}
void clear()
{
last = first;
}
void expand()
{
myVecto tmp;
tmp.first = new T[(end-first) * 2];
delete [] first;
}
void push_back(const T& val)
{
if(full())
{
expand();
}
*last++ = val;
}
void pop_back()
{
if(empty())
{
cout << "空,不可在删除" << endl;
}
else
{
last--;
}
}
void show()
{
for(auto i=this->first; i<this->last; i++)
{
cout << *i << " ";
}
cout << endl;
}
};
int main()
{
myVecto<int> v1(10, 1);
v1.show();
myVecto<int> v2 = v1;
v2.show();
cout << "vector.capacity = " << v2.size() << endl;
v2.push_back(3);
v2.show();
cout << "vector.capacity = " << v2.size() << endl;
return 0;
}
输出结果
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
vector.capacity = 10
14427280 0 15734736 0 1 1 1 1 1 1 3
vector.capacity = 11
循环队列
#include <iostream>
using namespace std;
class ERROR
{
private:
string msg;
public:
ERROR() {}
ERROR(string m):msg(m) {}
string what()
{
return msg;
}
};
template <typename T>
class circuqueue
{
private:
int* queue;
int size;
int front;
int rear;
public:
circuqueue() {};
circuqueue(int s):size(s+1), front(0), rear(0)
{
queue = new T[size];
}
~circuqueue()
{
delete [] queue;
queue = nullptr;
}
circuqueue(const circuqueue &other)
{
queue = new T[other.size];
front = other.front;
rear = other.rear;
int i = other.front;
for(; i!=other.rear; i=(i+1)%other.size)
{
queue[i] = other.queue[i];
}
}
circuqueue & operator=(const circuqueue &other)
{
if(this == &other)
{
return *this;
}
delete [] queue;
int i = other.front;
for(; i!=other.rear; i=(i+1)%other.size)
{
queue[i] = other.queue[i];
}
front = other.front;
rear = other.rear;
size = other.size;
return *this;
}
bool empty()
{
return front == rear;
}
bool full()
{
return front == (rear+1)%size;
}
void resize()
{
int *temp = new int[size * 2];
int j=0;
for(int i=front; i!=rear; i=(i+1)%size)
{
temp[j++] = queue[i];
}
front = 0;
rear = j;
delete []queue;
queue = temp;
temp = nullptr;
size *= 2;
}
void push(int val)
{
if(full())
{
resize();
}
queue[rear] = val;
rear = (rear + 1)%size;
}
void pop()
{
if(empty())
{
throw ERROR("队列不能为空");
}
front = (front+1)%size;
}
T & front1()
{
return queue[front];
}
T & back()
{
return queue[(rear-1+size)%size];
}
};
int main()
{
cout << "ninf" << endl;
circuqueue<int> q1;
cout << "cie" << endl;
for(int i=0; i<9; i++)
{
q1.push(i);
cout << q1.front1() << endl;
}
q1.pop();
for(int i=0; i<9; i++)
{
cout << q1.front1();
}
cout << endl;
return 0;
}