Java中的vector和list的區(qū)別和使用實(shí)例詳解-創(chuàng)新互聯(lián)

要了解vector,list,deque。我們先來(lái)了解一下STL。

成都創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的香坊網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

STL是Standard Template Library的簡(jiǎn)稱,中文名是標(biāo)準(zhǔn)模板庫(kù)。從根本上說(shuō),STL是一些容器和算法的集合。STL可分為容器(containers)、迭代器(iterators)、空間配置器(allocator)、配接器(adapters)、算法(algorithms)、仿函數(shù)(functors)六個(gè)部分。指針被封裝成迭代器,這里vector,list就是所謂的容器。

我們常常在實(shí)現(xiàn)鏈表,棧,隊(duì)列或者數(shù)組時(shí),都會(huì)寫(xiě)著一些重復(fù)或者相似的代碼,還要考慮各種可能出現(xiàn)的問(wèn)題。而STL的引入,大大提高了代碼的復(fù)用性。我們?cè)趯?shí)現(xiàn)這些代碼時(shí),只要引入頭文件就可以靈活的應(yīng)用了。

vector的使用

連續(xù)存儲(chǔ)結(jié)構(gòu):vector是可以實(shí)現(xiàn)動(dòng)態(tài)增長(zhǎng)的對(duì)象數(shù)組,支持對(duì)數(shù)組高效率的訪問(wèn)和在數(shù)組尾端的刪除和插入操作,在中間和頭部刪除和插入相對(duì)不易,需要挪動(dòng)大量的數(shù)據(jù)。它與數(shù)組大的區(qū)別就是vector不需程序員自己去考慮容量問(wèn)題,庫(kù)里面本身已經(jīng)實(shí)現(xiàn)了容量的動(dòng)態(tài)增長(zhǎng),而數(shù)組需要程序員手動(dòng)寫(xiě)入擴(kuò)容函數(shù)進(jìn)形擴(kuò)容。

Vector的模擬實(shí)現(xiàn)

template <class T>
class Vector
{
public:
  typedef T* Iterator;
  typedef const T* Iterator;
  Vector()
    :_start(NULL)
    ,_finish(NULL)
    ,_endOfStorage(NULL)
  {}
  void template<class T>
  PushBack(const T& x)
  {
    Iterator end = End();
    Insert(end, x);
  }
  void Insert(Iterator& pos, const T& x)
  {
    size_t n = pos - _start;
    if (_finish == _endOfStorage)
    {
      size_t len = Capacity() == 0 ? 3 :  Capacity()*2;
      Expand(len);
    }
    pos = _start+n;
    for (Iterator end = End(); end != pos; --end)
    {
      *end = *(end-1);
    }
    *pos = x;
    ++_finish;
  }
  Iterator End()
  {
    return _finish;
  }
  Iterator Begin()
  {
    return _start;
  }
  void Resize(size_t n, const T& val = T())//用Resize擴(kuò)容時(shí)需要初始化空間,并且可以縮小容量
  {
    if (n < Size())
    {
      _finish = _start+n;
    }
    else
    {
      Reserve(n);
      size_t len = n-Size();
      for (size_t i = 0; i < len; ++i)
      {
        PushBack(val);
      }
    }
  }
  void Reserve(size_t n)//不用初始化空間,直接增容
  {
    Expand(n);
  }
  inline size_t Size()
  {
    return _finish-_start;
  }
  inline size_t Capacity()
  {
    return _endOfStorage-_start;
  }
  void Expand(size_t n)
  {
    const size_t size = Size();
    const size_t capacity = Capacity();
    if (n > capacity)
    {
      T* tmp = new T[n];
      for (size_t i = 0; i < size; ++i)
      {
        tmp[i] = _start[i];
      }
      delete[] _start;
      _start = tmp;
      _finish = _start+size;
      _endOfStorage = _start+n;
    }
  }
  T& operator[](size_t pos)
  {
    assert(pos < Size());
    return _start[pos];
  }
  const T& operator[](size_t pos) const
  {
    assert(pos < Size());
    return _start[pos];
  }
protected:
  Iterator _start; //指向第一個(gè)元素所在節(jié)點(diǎn)
  Iterator _finish; //指向最后一個(gè)元素所在節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)
  Iterator _endOfStorage; //可用內(nèi)存空間的末尾節(jié)點(diǎn)
};

本文名稱:Java中的vector和list的區(qū)別和使用實(shí)例詳解-創(chuàng)新互聯(lián)
文章轉(zhuǎn)載:http://bm7419.com/article38/ddpppp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、服務(wù)器托管、標(biāo)簽優(yōu)化、動(dòng)態(tài)網(wǎng)站、App設(shè)計(jì)、App開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化