C++ STL:vector实现
练习一发,主要是使用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 ;
}
C++ STL:vector实现的更多相关文章
- C++ STL vector容器学习
STL(Standard Template Library)标准模板库是C++最重要的组成部分,它提供了一组表示容器.迭代器.函数对象和算法的模板.其中容器是存储类型相同的数据的结构(如vector, ...
- STL vector
STL vector vector是线性容器,它的元素严格的按照线性序列排序,和动态数组很相似,和数组一样,它的元素存储在一块连续的存储空间中,这也意味着我们不仅可以使用迭代器(iterator)访问 ...
- STL vector用法介绍
STL vector用法介绍 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和f ...
- STL vector+sort排序和multiset/multimap排序比较
由 www.169it.com 搜集整理 在C++的STL库中,要实现排序可以通过将所有元素保存到vector中,然后通过sort算法来排序,也可以通过multimap实现在插入元素的时候进行排序.在 ...
- STL vector 用法介绍
介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通 ...
- STL vector使用方法介绍
介绍 这篇文章的目的是为了介绍std::vector,怎样恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通 ...
- stl——vector详解
stl——vector详解 stl——vector是应用最广泛的一种容器,类似于array,都将数据存储于连续空间中,支持随机访问.相对于array,vector对空间应用十分方便.高效,迭代器使ve ...
- C++STL vector详解(杂谈)
介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通 ...
- C++ stl vector介绍
转自: STL vector用法介绍 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if ...
- 浅谈C++ STL vector 容器
浅谈C++ STL vector 容器 本篇随笔简单介绍一下\(C++STL\)中\(vector\)容器的使用方法和常见的使用技巧.\(vector\)容器是\(C++STL\)的一种比较基本的容器 ...
随机推荐
- Elasticsearch基础知识
ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口Elasticsearch是用Java开发的,并作为Apache ...
- [总结帖]Web小白的基础恶补帖
1. jQuery实现按钮点击跳转网页 <script src="js/jquery/jQuery-2.2.0.min.js" type="text/javascr ...
- Linux C 重定向简单范例
//前言:我们知道printf()会将信息输出到stdout流,然后展示到屏幕上. //那么我们所实现的简单的重定向就可以将stdout指向一个文件,然后读写,这样就达到了重定向的目的. //code ...
- centos启动错误:Inodes that were part of a corrupted orphan linked list found.
centos启动时,提示错误: /dev/mapper/VolGroup-lv_root contains a file system with errors,check forced. /dev/m ...
- appium安装与部署
前提: ①:appium属于C/S架构,代码写在Client端 ②:本章所说的部署讲的是Android设备下的Appium安装与部署 ③:Appium Client的环境是针对python3的 App ...
- python全栈开发_day16_包
一:包 1)包就是管理一系列模块的文件夹 2)包中有一个__init__.py文件来专门管理每一个模块(在__init__文件中不建议import导入模块,不建议as起别名) 二:导入完成的工作 1) ...
- 洛谷 P1273 有线电视网(树形背包)
洛谷 P1273 有线电视网(树形背包) 干透一道题 题面:洛谷 P1273 本质就是个背包.这道题dp有点奇怪,最终答案并不是dp值,而是最后遍历寻找那个合法且最优的\(i\)作为答案.dp值存的是 ...
- (二)Audio子系统之new AudioRecord()
在上一篇文章<(一)Audio子系统之AudioRecord.getMinBufferSize>中已经介绍了AudioRecord如何获取最小缓冲区大小,接下来,继续分析AudioReco ...
- 原创:centos7.1下 ZooKeeper 集群安装配置+Python实战范例
centos7.1下 ZooKeeper 集群安装配置+Python实战范例 下载:http://apache.fayea.com/zookeeper/zookeeper-3.4.9/zookeepe ...
- 四大组件之Activity Task任务栈4种启动模式
1.启动模式 standard,创建一个新的Activity. singleTop,栈顶不是该类型的Activity,创建一个新的Activity.否则,onNewIntent. singleTask ...