class A {
public:
int ma=10;
protected:
int mb;
private:
int mc;
};
class B:public A {
public:
void show() {
cout <<"B:ma"<< ma << endl;
cout <<"A::ma"<<A::ma << endl; }
int ma=20;
int md;
protected:
int me;
private:
int mf;
};
int main() {
B b;
cout << sizeof(b) << endl;
b.show();
return 0;
}
template<typename T>
class Allocator {//空间适配器
public:
//内存开辟
T* allocate(int size) {return (T*)malloc(sizeof(T)*size); }
//内存释放
void deallocate(T* p) { free(p); }
//对象构造
void construct(T* p, const T& val) { new (p)T(val); }
//对象析构
void destroy(T* p) { p->~T(); }
};
template <typename T,typename Alloc=Allocator<T>>
class vector {
public:
vector(int size = 10) {
_first = _allocator.allocate(size);
_last = _first;
_end = _first + size;
}
~vector() {
for (T* p = _first; p != _last; p++) {
_allocator.destroy(p);
}
_allocator.deallocate(_first);
_last = _first = _end = nullptr;
}
vector(const vector<T>& src) {
int size = src._end - src._first;
_first = _allocator.allocate(size);
int len = src._last - src._first;
for (int i = 0; i <len; i++) {
_allocator.construct(_first + i, src._first[i]);
}
_last = _first + len;
_end = _first + size;
}
vector<T>& operator=(const vector<T>& src) {
if (this == &src)return *this;
for (T* p = _first; p != _last; p++) {
_allocator.destroy(p);
}
_allocator.deallocate(_first);
int size = src._end - src._first;
_first = _allocator.allocate(size);
int len = src._last - src._first;
for (int i = 0; i < len; i++) {
_allocator.construct(_first + i,src._first[i]);
}
_last = _first + len;
_end = _first + size;
return *this;
}
int size()const { return _last - _first; }
void push_back(const T& val){
if (full()) {
resize();
}
_allocator.construct(_last, val);
_last++;
}
void pop_back() {
if (empty()) {
throw "vector is empty!!!";
}
--_last;
_allocator.destroy(_last);
}
bool empty()const { return _first == _last; }
bool full() { return _last == _end; }
T& operator[](int index) {
if (index < 0 || index >= size()) {
throw "OutOfRangeException!";
}
return _first[index];
}
T back() { return *(_last - 1); }
class iterator {
public:
iterator(T* p)
:_p(p)
{}
void operator++() { _p++; }
bool operator!=(const iterator& it) { return _p != it._p; }
T& operator*() { return *_p; }
private:
T* _p;
};
iterator begin() { return iterator(_first); }
iterator end() { return iterator(_last); }
private:
T* _first;
T* _last;
T* _end;
Alloc _allocator;
void resize();
};
template<typename T,typename Alloc>
void vector<T,Alloc>::resize() {
int size = _end - _first;
T* ptmp = _allocator.allocate(size*2);
for (int i = 0; i < size; i++) {
_allocator.construct(ptmp + i, _first[i]);
}
for (T* p = _first; p != _last; p++) {
_allocator.destroy(p);
}
_first = ptmp;
_last = _first + size;
_end = _first + size * 2;
}
class Test {
public:
Test() { cout << "Test()" << endl; }
~Test() { cout << "~Test()" << endl; }
Test(const Test& val) { cout << "Test(const Test& val)" << endl; }
};
int main() {
vector<int> vec;
for (int i = 0; i < 20; i++) {
vec.push_back(rand() % 100 + 1);
}
for (int i = 0; i <vec. size(); i++) {
cout << vec[i] << " ";
}
cout << endl;
vector<int>::iterator it = vec.begin();
for (; it != vec.end(); ++it) {
cout << *it << " ";
*it += 1;
}
cout << endl;
while (!vec.empty()) {
cout << vec.back() << " ";
vec.pop_back();
}
cout << endl;
Test t1, t2, t3;
vector<Test> vec1;
vec1.push_back(t1);
vec1.push_back(t2);
vec1.push_back(t3);
vec1.pop_back();
return 0;
}
#include <vector>
int main() {
vector<int> vec;
for (int i = 0; i < 20; i++) {
vec.push_back(rand() % 100 + 1);
}
for (int val : vec) {
cout << val << " ";
}
cout << endl;
vector<int>::iterator it = vec.begin();
while (it != vec.end()) {
if (*it % 2 == 0) {
it = vec.erase(it);//迭代器失效
}
else {
++it;
}
}
for (int val : vec) {
cout << val << " ";
}
cout << endl;
it = vec.begin();
while (it != vec.end()) {
if (*it % 5 == 0) {
it=vec.insert(it, *it + 3);//迭代器失效
++it;
}
++it;
}
for (int val : vec) {
cout <<val << " ";
}
cout << endl;
}
template <typename T>
class Queue {
public:
Queue() { _front = _rear = new QueueItem(); }
~Queue() {
QueueItem* cur = _front;
while (cur != nullptr) {
_front = cur->_next;
delete cur;
cur = _front;
}
_front = _rear = nullptr;
}
void push(const T& val) {
QueueItem* item = new QueueItem(val);
_rear->_next = item;
_rear = item;
}
void pop() {
if (empty()) { throw "queue is empty!!!"; }
QueueItem* first = _front->_next;
_front->_next = first->_next;
if (_front->_next == nullptr) {
_rear = _front;
}
delete first;
}
T front()const {
if (empty()) { throw "queue is empty!!!"; }
return _front->_next->_data;
}
bool empty()const { return _front == _rear; }
private:
struct QueueItem {
QueueItem(T data=T())
:_data(data)
,_next(nullptr)
{}
T _data;
QueueItem* _next;
static QueueItem* _itemPool;
static const int POOL_ITEM_SIZE = 100000;
void* operator new(size_t size){
if (_itemPool == nullptr) {
_itemPool = (QueueItem*)new char[sizeof(QueueItem) * POOL_ITEM_SIZE];
QueueItem* p = _itemPool;
for (; p < _itemPool + POOL_ITEM_SIZE - 1; p++) {
p->_next = p + 1;
}
p->_next = nullptr;
}
QueueItem* p = _itemPool;
_itemPool = _itemPool->_next;
return p;
}
void operator delete(void* ptr) {
QueueItem* p = (QueueItem*)ptr;
p->_next = _itemPool;
_itemPool = p;
}
};
QueueItem* _front;
QueueItem* _rear;
};
template <typename T>
typename Queue<T>::QueueItem* Queue<T>::QueueItem::_itemPool = nullptr;
int main() {
Queue<int> q;
for (int i = 0; i < 100000000; i++) {
q.push(rand() % 1000000 + 1);
q.pop();
}
return 0;
}