练习一发,主要是使用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实现的更多相关文章

  1. C++ STL vector容器学习

    STL(Standard Template Library)标准模板库是C++最重要的组成部分,它提供了一组表示容器.迭代器.函数对象和算法的模板.其中容器是存储类型相同的数据的结构(如vector, ...

  2. STL vector

    STL vector vector是线性容器,它的元素严格的按照线性序列排序,和动态数组很相似,和数组一样,它的元素存储在一块连续的存储空间中,这也意味着我们不仅可以使用迭代器(iterator)访问 ...

  3. STL vector用法介绍

    STL vector用法介绍 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和f ...

  4. STL vector+sort排序和multiset/multimap排序比较

    由 www.169it.com 搜集整理 在C++的STL库中,要实现排序可以通过将所有元素保存到vector中,然后通过sort算法来排序,也可以通过multimap实现在插入元素的时候进行排序.在 ...

  5. STL vector 用法介绍

    介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通 ...

  6. STL vector使用方法介绍

    介绍 这篇文章的目的是为了介绍std::vector,怎样恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通 ...

  7. stl——vector详解

    stl——vector详解 stl——vector是应用最广泛的一种容器,类似于array,都将数据存储于连续空间中,支持随机访问.相对于array,vector对空间应用十分方便.高效,迭代器使ve ...

  8. C++STL vector详解(杂谈)

    介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通 ...

  9. C++ stl vector介绍

    转自: STL vector用法介绍 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if ...

  10. 浅谈C++ STL vector 容器

    浅谈C++ STL vector 容器 本篇随笔简单介绍一下\(C++STL\)中\(vector\)容器的使用方法和常见的使用技巧.\(vector\)容器是\(C++STL\)的一种比较基本的容器 ...

随机推荐

  1. java使用memcached1--安装与基本使用

    环境 CentOs6.4 libevent-2.0.22-stable memcached-1.4.24 一.memcached安装 # cd /usr/local 1.编译安装libevent # ...

  2. 安装gensim报错:Original error was: DLL load failed: 找不到指定的模块。 Command "python setup.py egg_info" failed with error code 1 in C:\Users\xubing\AppData\Local\Temp\pip-install-nta89iep\gensim\

    1.pip install --upgrade setuptools #安装或升级 2.如果是基于numpy的python 包,升级numpy pip install -U numpy 3.重新pip ...

  3. 使用HttpClient出现java.io.IOException: Attempted read from closed stream

    问题描述: 使用httpClient时候,出现java.io.IOException: Attempted read from closed stream. 原始代码: public static S ...

  4. 关于Mysql数据库查询数据大小写的问题汇总

    前天在问答区看到一个童鞋对于mysql中大小写问题不熟悉,在回复他后再次汇总梳理如下: mysql中大小写问题主要有以下两种: A.表名区分大小写 ower_case_table_names 是表名区 ...

  5. docker微服务部署之:二、搭建文章微服务项目

    docker微服务部署之:一,搭建Eureka微服务项目 一.新增demo_article模块,并编写代码 右键demo_parent->new->Module->Maven,选择M ...

  6. leetcode-278-First Bad Version(注意不要上溢)

    题目描述:(说明中有简单翻译) You are a product manager and currently leading a team to develop a new product. Unf ...

  7. (原创推荐文章)kerberos服务器端与客户端

    #环境 两台装centos7的虚拟机即可. kerberos服务器端与客户端各一台 (本文档推荐使用Typora软件观看) # 1.kerberos服务器端配置 ## 1.1安装配置Kerberos ...

  8. 利用atimicInteger cas的特性实现一个锁

    利用atimicInteger cas的特性实现一个锁 主要是使用的是 atomicIntegerAPI 的compareAndSet()方法,让线程不在阻塞,获取不到直接失败. 我们先定义一个异常类 ...

  9. Hibernate 工具类

    1.HibernateConfigUtil.java(HIbernate配置工具类) import org.hibernate.Session; import org.hibernate.Sessio ...

  10. LeetCode一句话题解

    深度优先搜索 人生经验 1. 需要输出所有解.并由于元素集有重复元素,要求返回的结果需要去重的情况,可考虑使用值对应数量的map,然后分别考虑依次取不同数量该值的可能. LeetCode39 题目:给 ...