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 = 0;

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-1);}

    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 == 0 ? 1 : olen * 2;
            T* new_start = (T*) malloc(nlen * sizeof(T));
            T* new_finish = new_start;
            for (int i=0; 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=0; 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 + 1;
    int n = (char*)b - (char*)a;

    cout<<n<<endl;

    int last_capacity =-1;
    Object::count = 0;

#if USE_ORG
    vector<Object> tmp;
#else
    MyVector<Object> tmp;
#endif

    for (int i=0; i<5; 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(2);
    ints.push_back(3);
    ints.push_back(1);

    sort(ints.begin(), ints.end());

    for (int i:ints) {
        cout<<i<<endl;
    }

    system("pause");
    return 0;
}
时间: 2024-12-17 18:47:27

C++ STL:vector实现的相关文章

STL vector中的rbegin方法(5)

public member function <vector> std::vector::rbegin C++98 C++11 reverse_iterator rbegin() noexcept; const_reverse_iterator rbegin() const noexcept; Return reverse iterator to reverse beginning 返回一个反向的首元素. 例子: #include <iostream> #include <v

C++ STL vector容器学习

STL(Standard Template Library)标准模板库是C++最重要的组成部分,它提供了一组表示容器.迭代器.函数对象和算法的模板.其中容器是存储类型相同的数据的结构(如vector,list, deque, set, map等),算法完成特定任务,迭代器用来遍历容器对象,扮演容器和算法之间的胶合剂. 模板类vector 在计算中,矢量(vector)对应数组,它的数据安排以及操作方式,与array非常类似.在C++中,使用vector模板类时,需要头文件包含#include<v

STL vector用法介绍

介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通过阅读这篇文章读者应该能够有效地使用vector容器,而且应该不会再去使用C类型的动态数组了.   Vector总览 vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象

STL ——vector 学习

STL简介 C++ STL (Standard Template Library标准模板库) 是通用类模板和算法的集合,它提供给程序员一些标准的数据结构的实现如 queues(队列), lists(链表), 和 stacks(栈)等.  C++ STL 提供给程序员以下三类数据结构的实现: 标准容器类   顺序性容器  vector 从后面快速的插入与删除,直接访问任何元素  deque 从前面或后面快速的插入与删除,直接访问任何元素 list 双链表,从任何地方快速插入与删除   关联容器  

STL vector的介绍(1)

尝试下翻译STL里面的一些容易和算法.四级过了,六级刚考.顺便练练自己的英语水平,翻译的不好的地方请大神多多指教哈,方便我改正. 原来均来自:http://www.cplusplus.com/ template < class T, class Alloc = allocator<T> > class vector; // generic template Vector Vectors are sequence containers representing arrays that

C++ stl vector介绍

转自: STL vector用法介绍 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通过阅读这篇文章读者应该能够有效地使用vector容器,而且应该不会再去使用C类型的动态数组了.   Vector总览 vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是

STL vector

author:Donald-hu    theme:STL vector 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通过阅读这篇文章读者应该能够有效地使用vector容器,而且应该不会再去使用C类型的动态数组了.   Vector总览 vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.v

STL vector使用方法介绍

介绍 这篇文章的目的是为了介绍std::vector,怎样恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通过阅读这篇文章读者应该可以有效地使用vector容器,并且应该不会再去使用C类型的动态数组了.   Vector总览 vector是C++标准模板库中的部分内容,它是一个多功能的,可以操作多种数据结构和算法的模板类和函数库.vector之所以被觉得是一个容器,是由于它可以像容器一样存放各种类型的对象

STL vector的构造函数和析构函数(2)

原文来自:点击打开链接 译文: public member function vector的构造器:这里我只翻译C++11的,C++98的就不翻译了. 构造器原型: <vector> std::vector::vector C++98 C++11 default (1) explicit vector (const allocator_type& alloc = allocator_type()); fill (2) explicit vector (size_type n); vec

STL vector用法介绍(转)

介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通过阅读这篇文章读者应该能够有效地使用vector容器,而且应该不会再去使用C类型的动态数组了.   Vector总览 vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象