练习一发,主要是使用placement new在原始内存上创建对象。半路md面试电话来了,赶紧存档,看Java大法

#include <iostream>
#include <cstdlib>
#include <vector>
#include <algorithm> using namespace std; class Object {
public:
static int count;
public:
int current;
int id;
Object(int i) {
id = i;
current = count++;
cout<<"object["<<id<<"] create : "<<current<<endl;
} ~Object() {
cout<<"object["<<id<<"] destroy: "<<current<<endl;
} Object(const Object& obj) {
current = count++;
id = obj.id;
cout<<"object["<<id<<"] copied : "<<current<<" old : "<<obj.current<<endl;
}
}; int Object::count = ; template<class T>
class MyVector {
public:
typedef T value_type;
typedef value_type* pointer;
typedef value_type* iterator;
typedef value_type& reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type; private:
iterator start;
iterator finish;
iterator space_end;
public:
MyVector() {
start = NULL;
space_end = NULL;
finish = NULL;
} size_type capacity() {return space_end - start;}
size_type size() { return finish - start; } bool empty() { return start == finish; }
iterator begin() {return start;}
iterator end() {return finish;} reference operator[] (int idx) {return start[idx];}
reference front() {return *start;}
reference back() {return *(finish-);} void clear() { } void pop_back() {
if (start >= finish) {
return;
}
finish--;
finish->~T();
} void push_back(const T& x) {
if (finish < space_end) {
// placement new
new (finish) T(x);
// adjust end pointer
finish++;
} else if (space_end == finish){
// space not enough
int olen = finish - start;
int nlen = olen == ? : olen * ;
T* new_start = (T*) malloc(nlen * sizeof(T));
T* new_finish = new_start;
for (int i=; i<olen; i++) {
// copy old data to new space
new (new_finish++) T(*(start + i));
}
// append pushed element
new (new_finish++) T(x); // deconstruct old ones
for (int i=; i<olen; i++) {
(start+i)->~T();
}
free(start); start = new_start;
finish= new_finish;
space_end = start + nlen;
} else {
// state invalid
cerr<<"error"<<endl;
}
} ~MyVector() {
for (T* s=start; s<finish; s++) {
s->~T();
}
free(start);
}
}; #define USE_ORG 0
int main() { long* a = NULL;
long* b = a + ;
int n = (char*)b - (char*)a; cout<<n<<endl; int last_capacity =-;
Object::count = ; #if USE_ORG
vector<Object> tmp;
#else
MyVector<Object> tmp;
#endif for (int i=; i<; i++) {
tmp.push_back(Object(i));
if (last_capacity != tmp.capacity()) {
cout<<"======last cap:"<<last_capacity<<" new capacity:"<<tmp.capacity()<<endl;
last_capacity = tmp.capacity();
} cout<<"\n\n"<<endl;
} MyVector<int> ints;
ints.push_back();
ints.push_back();
ints.push_back(); sort(ints.begin(), ints.end()); for (int i:ints) {
cout<<i<<endl;
} system("pause");
return ;
}

最新文章

  1. UILabel 自适应宽高
  2. java Properties 配置信息类
  3. UEFI Bootable USB Flash Drive - Create in Windows(WIN7 WIN8)
  4. JS匿名函数的理解
  5. CLR和JIT
  6. Ajax 异步调用代码
  7. htaccess 探秘
  8. [原创]obj-c编程16:键值编码(KVC)
  9. python 全栈开发,Day5
  10. 利用国外服务器搭建ss
  11. 列表(list)之二 -运用篇 -快速生成规律性列表
  12. 洛谷 P1045 &amp; [NOIP2003普及组] 麦森数
  13. window.print()小知识
  14. (转)android import library switch语句报错case expressions must be constant expressions
  15. Mongodb的下载和安装
  16. _itemmod_enchant_groups
  17. Intellj(IDEA)创建java webapp
  18. 在使用SQLServer时忘记sa账号密码解决办法
  19. Linux &amp; Oracle目录说明
  20. MS12-020蓝屏攻击

热门文章

  1. mysql 添加索引 mysql 如何创建索引
  2. Android 线刷小白教程
  3. Enum枚举法
  4. CentOS7打开关闭防火墙与端口
  5. 【Qt开发】实现系统托盘,托盘菜单,托盘消息
  6. jmeter的non-gui模式的使用
  7. 浅谈mongodb与Python的交互
  8. java中复制bean
  9. HAL库定时器
  10. python全栈开发_day17_时间,系统模板和序列化