zoukankan      html  css  js  c++  java
  • 数据结构-Vector

    自定义Vector实现:

    ///////////////////////////////////////////////////////////////////////////////
    //
    //  FileName    :   c2_vector.h
    //  Author      :   Jimmy Han
    //  Date        :   N.A                 v1    
    //              :   2014/07/13 09:30    v2    
    //
    ///////////////////////////////////////////////////////////////////////////////
    #include <cassert>
     
    template <class object>
    class Vector {
    public:
        explicit Vector(int initsize = 0)
            : _size(initsize), _capacity(initsize + SPARE_CAPACITY)
                {_object = new object[_capacity];}
    
        Vector(const Vector &rhs) : _object(NULL) 
            { operator=(rhs);}
            
        ~Vector()
            {delete [] _object;}
            
        const Vector& operator=(const Vector &rhs)
        {
            if(this != &rhs)
                {
                    delete [] _object;
                    _size = rhs._size;
                    _capacity = rhs._capacity;
                    
                    _object = new object[_capacity];
                    for(int k = 0; k < _size; k++)
                        _object[k] = rhs._object[k];    
                }
                return *this;
        }
        
        void resize(int newSize)
        {
            if(newSize > _capacity)
                reserve(newSize * 2 + 1);
            _size = newSize;
        }
        
        void reserve(int newCapacity)
        {
            if(newCapacity < _size)
                return;
            
            object *oldobject = _object;
            _object = new object[newCapacity];
            for (int n=0; n<_size; n++)
                _object[n] = oldobject[n];
            delete [] oldobject;
            _capacity = newCapacity;
        }
        
        object& operator[](const int index)
        {
            assert(index >= 0 && index < _size);
            return _object[index];
        }
        
        const object& operator[](const int index) const
        {
            assert(index >= 0 && index < _size);
            return _object[index];
        }
        
        bool empty() const 
        {return _size == 0;}
        
        int size() const 
        {return _size;}
        
        int capacity() const 
        {return _capacity;}
        
        void push_back(const object &x)
        {
            if(_size == _capacity)
                reserve(2*_capacity + 1);
            _object[_size++] = x;
        }
        
        void pop_back()
            {_size--;}
        
        const object& back() const 
            { return _object[_size - 1];}
        
        typedef object* iterator ;
        typedef const object* const_iterator;
        
        iterator begin()
        {return &_object[0];}
        
        iterator end()
        {return     &_object[_size];}
    
        const_iterator begin() const
        {return &_object[0];}
        
        const_iterator end() const
        {return &_object[_size];}    
    
        enum{ SPARE_CAPACITY = 3};    
    private:
        int _size;
        int _capacity;
        object *_object;    
    };

    自定义Vector测试程序:

    ///////////////////////////////////////////////////////////////////////////////
    //
    //  FileName    :   c2_vector.cpp
    //  Author      :   Jimmy Han
    //  Date        :   N.A 			    v1    
    //              :   2014/07/13 09:30    v2    
    // $ ./a.exe
    // Calculate the size of self made Vector class
    // sizeof(vint) is: 12
    
    // Self made Vector size test:
    // size of vint is: 5
    // capacity of vint is: 7
    // size of vint2 is: 8
    // capacity of vint2 is: 17
    // the last element of vint is: 4
    // the 5 element of vint is: 4
    
    // Self made iterator test:
    // 0 1 2 3
    
    // Iterator called by others:
    // [0 1 2 3]
    // [0 1 2 3 4 1919249516 1547322171 1735357008]
    //
    ///////////////////////////////////////////////////////////////////////////////
    #include <iostream>
    #include "printCollection.h"
    #include "c2_vector.h"
    using namespace std;
    int main()
    {
    	//default constructor
    	Vector<int> vint;
    	Vector<int> vint2;
    	
    	//calc class size, two int, one pointer, so size is 12
    	cout << "Calculate the size of self made Vector class " << endl;
    	cout <<"sizeof(vint) is: " << sizeof(vint) << endl << endl;
    	
    	//void push_back(const object &x)
    	for (int n=0; n<5; n++)
    		vint.push_back(n);
    	
    	//const Vector& operator=(const Vector &rhs)
    	vint2 = vint;
    	
    	//void resize( int newSize)
    	vint2.resize(8);
    	
    	//int size(); int capacity();
    	cout << "Self made Vector size test: " << endl;
    	cout << "size of vint is: " << vint.size() << endl;
    	cout << "capacity of vint is: " << vint.capacity() << endl;
    	cout << "size of vint2 is: " << vint2.size() << endl;
    	cout << "capacity of vint2 is: " << vint2.capacity() << endl;
    	
    	//const object& back();
    	cout << "the last element of vint is: " << vint.back() << endl;
    	
    	//object& operator[](const int index)
    	cout << "the 5 element of vint is: " << vint[4] << endl << endl;
    	
    	//void pop_back()
    	vint.pop_back();
    	
    	//typedef object* iterator; typedef const ojbect* const_iterator
    	//iterator begin(); iterator end();
    	cout << "Self made iterator test: " << endl;
    	for (Vector<int>::iterator iter = vint.begin(); iter != vint.end(); iter++)
    	{
    		cout << *iter << " ";
    	}
    	cout << endl << endl;
    	
    	cout << "Iterator called by others: " << endl;
    	printCollection(vint); 
    	//after resize, not initilized member got random number
    	printCollection(vint2); 
    }
    
  • 相关阅读:
    linux磁盘简介
    linux用户管理、su和sudo、文件权限、SUID/SGID/SBIT
    dubbo框架的使用方法。。。
    Linux 配置 dubbo 和 dubbo的简单介绍。
    单列模式简单介绍
    Linux配置zookeeper 和zookeeper简单介绍
    Linux配置 ftp 和 ftp简单介绍
    正向代理 、反向代理, 和 Linux系统配置nginx。
    Linux配置Redis集群 和 缓存介绍。
    Linux 集群 和免秘钥登录的方法。
  • 原文地址:https://www.cnblogs.com/dracohan/p/3825933.html
Copyright © 2011-2022 走看看