vector和list的使用及其模拟实现

一.vector使用及模拟实现。

1.vector简介。
vector和数组类似,拥有一段连续的内存空间,并且起始地址不变。
因此能高效的进行随机存取,时间复杂度为o(1);
但因为内存空间是连续的,所以在进行插入和删除操作时,会造成内存块的拷贝,时间复杂度为o(n)。
另外,当数组中内存空间不够时,会重新申请一块内存空间并进行内存拷贝。

注:vector相当于一个动态增长的数组。

2.vector的一些基本用法。
(1).头文件包含。
使用时要在前面加上#include<vector>以包含所需的类文件vector。
(2).变量声明。
1>.声明一个int向量以替代一个一维数组:vector<int> a; 即声明了一个一维数组a[ ],数组大小没有给定,可以动态向里面添加或者删除。
2>.声明一个数组向量以代替一个二维数组:vector<int *> a;
3.具体的用法及函数调用。
用法:
(1).push_back 在数组的最后添加一个数据
(2).pop_back 去掉数组的最后一个数据
(3).at 得到编号位置的数据
(4).begin 得到数组头的指针
(5).end 得到数组的最后一个单元+1的指针
(6).front 得到数组头的引用
(7).back 得到数组的最后一个单元的引用
(8).max_size 得到vector最大可以是多大
(9).capacity 当前vector分配的大小
(10).size 当前使用数据的大小
(11).resize 改变当前使用数据的大小,如果它比当前使用的大,者填充默认值
(12).reserve 改变当前vecotr所分配空间的大小
(13).erase 删除指针指向的数据项
(14).clear 清空当前的vector
(15).rbegin 将vector反转后的开始指针返回(其实就是原来的end-1)
(16).rend 将vector反转构的结束指针返回(其实就是原来的begin-1)
(17).empty 判断vector是否为空
(18).swap 与另一个vector交换数据
详细的函数实现功能:其中vector<int> v.
v.clear() 移除容器中所有数据。
v.empty() 判断容器是否为空。
v.erase(pos) 删除pos位置的数据
v.erase(beg,end) 删除[beg,end)区间的数据
v.front() 传回第一个数据。
v.insert(pos,elem) 在pos位置插入一个elem拷贝
v.pop_back() 删除最后一个数据。
v.push_back(elem) 在尾部加入一个数据。
v.resize(num) 重新设置该容器的大小
v.size() 回容器中实际数据的个数。
v.begin() 返回指向容器第一个元素的迭代器
v.end() 返回指向容器最后一个元素的迭代器

注:具体用法和函数的调用会在后面的代码中详细体现

4.内存管理与效率。
(1)使用reserve()函数提前设定容量大小,避免多次容量扩充操作导致效率低下。
(2)size()告诉你容器中有多少元素。它没有告诉你容器为它容纳的元素分配了多少内存。
(3)capacity()告诉你容器在它已经分配的内存中可以容纳多少元素。那是容器在那块内存中总共可以容纳多少元素,而不是还可以容纳多少元素。如果你想知道一个vector或string中有多少没有被占用的内存,你必须从capacity()中减去size()。如果size和capacity返回同样的值,容器中就没有剩余空间了,而下一次插入(通过insert或push_back等)会引发上面的重新分配步骤。
(4)resize(Container::size_type n)强制把容器改为容纳n个元素。调用resize之后,size将会返回n。如果n小于当前大小,容器尾部的元素会被销毁。如果n大于当前大小,新默认构造的元素会添加到容器尾部。如果n大于当前容量,在元素加入之前会发生重新分配。
(5)reserve(Container::size_type n)强制容器把它的容量改为至少n,提供的n不小于当前大小。这一般强迫进行一次重新分配,因为容量需要增加。
5.vector的模拟实现。
代码:

Vector.h

#pragma once

#include<vector>

void print_vector(const vector<int>& v)
{
    vector<int>::const_iterator it = v.begin();
    while(it != v.end())
    {
        cout<<*it<<" ";
        ++it;
    }
    cout<<endl;

    for(size_t i = 0;i < v.size();++i)
    {
        cout<<v[i]<<" ";
    }
    cout<<endl;
}

void test_vector1()
{
    vector<int> v1;
    v1.push_back(1);
    v1.push_back(2);
    v1.push_back(3);
    v1.push_back(4);

    vector<int>::iterator it = v1.begin();
    while(it != v1.end())
    {
        if(*it % 2 == 0)
        {
            it=v1.erase(it);
        }
        else
        {
            ++it;
        }
    }
    print_vector(v1);
}

void test_vector2()
{
    vector<int> v2;
    v2.push_back(1);
    v2.push_back(2);
    v2.push_back(3);
    v2.push_back(4);
    v2.push_back(5);
    v2.push_back(6);
    v2.push_back(7);
    v2.push_back(8);
    v2.push_back(9);

    print_vector(v2);
    cout<<"capacity:"<<v2.capacity()<<endl;

    v2.resize(3);
    /*v2.resize(10,10);*/

    print_vector(v2);
    cout<<"capacity:"<<v2.capacity()<<endl;
}

void test_vector3()
{
    vector<int> v3;
    v3.push_back(1);
    v3.push_back(2);
    v3.push_back(3);
    v3.push_back(4);
    v3.push_back(5);
    v3.push_back(6);
    v3.push_back(7);
    v3.push_back(8);
    v3.push_back(9);

    print_vector(v3);
    cout<<"capacity:"<<v3.capacity()<<endl;

        v3.reserve(20);

    print_vector(v3);
    cout<<"capacity:"<<v3.capacity()<<endl;
}



template<class T>
class Vector
{
public:
    //typedef T* Iterator;
    typedef const T* Iterator;

   Vector()
       :_start(NULL)
       ,_finish(NULL)
       ,_endOfStorage(NULL)
   {}

   void PushBack(const T& x)
   {
       Iterator end = End();
       Insert(end,x);
   }

   void PopBack()
   {}

   void Insert(Iterator& pos,const T& x)
   {
       size_t n = pos - _finish;
       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())
   {
       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;
    Iterator _finish;
    Iterator _endOfStorage;
};

void TestVector()
{
    Vector<int> v;
    v.PushBack(1);
    v.PushBack(2);
    v.PushBack(3);
    v.PushBack(4);

    Vector<int>:: Iterator pos = v.Begin();
    v.Insert(pos,10);
    v.Insert(pos,20);
    v.Insert(pos,30);
    v.Insert(pos,40);

    Vector<int>:: Iterator it = v.Begin();
    while(it!=v.End())
    {
        cout<<*it<<" ";
        ++it;
    }
      cout<<endl;
}

TestVector.cpp

#include<iostream>
using namespace std;

#include "Vector.h"


int main()
{
    //test_vector1();
    test_vector2();
    //test_vector3();
    return 0;
}

二.list的使用及模拟实现。

1.list的简介。
list是一个线性双向链表结构,它的数据由若干个节点构成,每一个节点都包括一个信息块(即实际存储的数据)、一个前驱指针和一个后驱指针。它无需分配指定的内存大小且可以任意伸缩,这是因为它存储在非连续的内存空间中,并且由指针将有序的元素链接起来。由于其结构的原因,list 随机检索的性能非常的不好,因为它不像vector 那样直接找到元素的地址,而是要从头一个一个的顺序查找,这样目标元素越靠后,它的检索时间就越长。检索时间与目标元素的位置成正比。虽然随机检索的速度不够快,但是它可以迅速地在任何节点进行插入和删除操作。因为list 的每个节点保存着它在链表中的位置,插入或删除一个元素仅对最多三个元素有所影响,不像vector 会对操作点之后的所有元素的存储地址都有所影响,这一点是vector 不可比拟的。

注:list相当于一个双向循环链表。

2.list 的特点:
(1) 不使用连续的内存空间这样可以随意地进行动态操作;
(2) 可以在内部任何位置快速地插入或删除,当然也可以在两端进行push 和pop 。
(3) 不能进行内部的随机访问,即不支持[ ] 操作符和vector.at() ;
(4) 相对于verctor 占用更多的内存。

3.初学list:需要掌握的知识:
(1)定义一个list .
(2)向list中加入元素 .
(3)如何知道list是否为空 .
(4)如何使用for循环来遍历一个list .
(5)如何使用STL的通用算法for_each来遍历list.
(6)list成员函数begin() 和 end() 以及它们的意义 .
(7)iterator范围的概念和一个范围的最后一个位置实际上并不被处理这一事实 .
1>.第一:定义,插入,遍历打印。

代码实现如下:

#include<stdio.h>
#include<iostream>
#include<list>
#include<string>
#include<algorithm>
using namespace std;



void PrintIt(string& StringToPoint)
{
cout << StringToPoint << endl;
}

int main()

{

list<string> test;

list<string>::iterator testiterator;

test.push_back("no");
test.push_back("march");
test.push_front("ok");
test.push_front("loleina");
test.push_front("begin");
test.push_back("end");



for (testiterator = test.begin(); testiterator != test.end(); ++testiterator)
{
cout << *testiterator << endl;
}
cout << "-------------" << endl;


for_each(test.begin(), test.end(), PrintIt);
cout << "-------------" << endl;

system("PAUSE");
return 0;
}

定义了一个字符串类型的list。需要包含提供STL的 list类的头文件#include <list>即可;list的成员函数push_back()把一个对象放到一个list的后面,而 push_front()把对象放到前面。

我们想要遍历一个list,比如打印一个list中的所有对象来看看list上不同操作的结果。要一个元素一个元素的遍历一个list, 可以这样做:

A. 这个程序定义了一个iterator(类似指针),testiterator。它指向了这个list的第一个元素。 这可以调用testiterator.begin()来做到,它会返回一个指向list开头的iterator。然后把它和testiterator.end()的 返回值来做比较,到了那儿的时候就停下来。 容器的end()函数会返回一个指向容器的最后一个位置的iterator。 在上面的例子中,每一次执行for循环,我们就重复引用iterator来得到我们打印的字符串。

注意:不能用testiterator.begin()+2来指向list中的第三个对象,因为STL的list是以双链的list来实现的,所有的数据存放不一定是连续存放的。 它不支持随机存取。

B.使用STL的通用算法for_each()来遍历一个iterator的范围,然后调用PrintIt()来处理每个对象。 不需要初始化、比较和给iterator增量。for_each()完成了这些工作。执行于对象上的操作被很好的 打包在这个函数以外了,不用再做那样的循环了,代码更加清晰了。

第二:count()和count_if() 的基本使用

STL的通用算法count()和count_it()用来给容器中的对象记数。就象for_each()一样,count()和count_if() 算法也是在iterator范围内来做的。
#include<stdio.h>
#include<iostream>
#include<list>
#include<string>
#include<algorithm>
using namespace std;

class IsLoleina
{
public:
bool operator()(string& name)
{
return name == "loleina";
}

};

int main()
{
list<string> test;
list<int> score;
list<string>::iterator testiterator;

test.push_back("no");
test.push_back("march");
test.push_front("ok");
test.push_front("loleina");
test.push_front("begin");
test.push_back("end");


score.push_back(100);
score.push_back(90);
score.push_back(80);
score.push_back(70);
score.push_back(100);
score.push_back(20);


int countNum(0);

countNum= count(score.begin(), score.end(), 100);
cout << "there are " << countNum << " scores of 100" << endl;
cout << "-------------" << endl;

int countLoleina(0);
countLoleina=count_if(test.begin(), test.end(), IsLoleina());
cout << "there are " << countLoleina << " loleina" << endl;


system("PAUSE");
return 0;

count()算法统计等于某个值的对象的个数。count_if() 带一个函数对象的参数。函数对象是一个至少带有一个operator()方法的类。有些STL算法作为参数接收函数对象并调用这个函数对象的operator()方法。函数对象被约定为STL算法调用operator时返回true或false。它们根据这个来判定这个函数。举个例子会 说的更清楚些。count_if()通过传递一个函数对象来作出比count()更加复杂的评估以确定一个对象是否应该被记数。
4.list的模拟实现。
代码如下:
List.h

#pragma once

#include<list>
#include<assert.h>
#include <algorithm>


void print_list(const list<int>& l)
{
    list<int>::const_iterator it = l.begin();
    while(it != l.end())
    {
        cout<<*it<<" ";
        it++;
    }
    cout<<endl;
}

//void test_list()
//{
//  list<int> l1;
//  l1.push_back(1);
//  l1.push_back(2);
//  l1.push_back(3);
//  l1.push_back(4);
//
//  print_list(l1);
//
//  list<int>::iterator it1 = l1.begin();
//  while(it1 != l1.end())
//  {
//      cout<<*it1<<" ";
//      if(*it1 % 2 == 0)
//      {
//          (*it1)*=2;
//      }
//      it1++;
//  }
//  cout<<endl;
//
//  print_list(l1);
//
//
//  list<int>::reverse_iterator it2 = l1.rbegin();
//  while(it2!=l1.rend())
//  {
//      cout<<*it2<<" ";
//      it2++;
//  }
//  cout<<endl;
//}


//void test_list()
//{
//  list<int> l1;
//  l1.push_back(1);
//  l1.push_back(2);
//  l1.push_back(3);
//  l1.push_back(4);
//   
//  //删除所有偶数
//  //迭代器失效==产生野指针
//  list<int>::iterator it1 = l1.begin();
//  while(it1 != l1.end())
//  {
//      if(*it1 % 2 == 0)
//      {
//          it1 = l1.erase(it1);//erase有返回值,返回最近删除元素的下一个位置
//      }
//      else
//      {
//          ++it1;
//      }
//  }
//     print_list(l1);
//
//   cout<<l1.max_size()<<endl;//size_t (-1)/sizeof(int)
//   list<char> l2;
//   cout<<l2.max_size()<<endl;//size_t(-1)/sizeof(char)
//}


//void test_list()
//{
//  list<int> l1;
//  l1.push_back(1);
//  l1.push_back(2);
//  l1.push_back(3);
//  l1.push_back(4);
//
//  print_list(l1);
//
//  l1.assign(10,1);//clear + push_back
//  print_list(l1);
//
//  list<int> l2;
//  l2.push_back(10);
//  l2.push_back(20);
//  l2.push_back(20);
//  l2.push_back(30);
//
//  l1.assign(++l2.begin(),--l2.end());
//  print_list(l1);
//
//  string s("你好");
//  l1.assign(s.begin(),s.end());
//  print_list(l1);
//  char buf[5]={0};
//  buf[0] = -60;
//  buf[1] = -29;
//  buf[2] = -70;
//  buf[3] = -61; 
//
//  cout<<buf<<endl;
//  print_list(l1);
//}


template<class T>
struct __ListNode
{
    T _data;
    __ListNode<T>* _next;
    __ListNode<T>* _prev;

    __ListNode(const T& x)
        :_data(x)
        ,_next(NULL)
        ,_prev(NULL)
    {}
};


template <class T,class Ref,class Ptr>
struct __ListIterator
{
    typedef __ListNode<T> Node;
    typedef __ListIterator<T,Ref,Ptr> Self;

    __ListIterator(Node* node)
        :_node(node)
    {}

    Ref operator *()
    {
        return _node->_data;
    }

    Ptr operator ->()
    {
        return  &(_node->_data);
    }

    bool operator== (const Self &s)
    {
        return _node == s._node;
    }

    bool operator!=(const Self &s)
    {
        return _node!=s._node
    }
    Self& operator++()
    {
        _node = _node->_next;
        return *this;
    }

    Self& operator--()
    {
        _node = _node->_prev;
        return *this;
    }
};
template<class T>
class List
{
    typedef __ListNode<T>  Node;
public:
    typedef __ListIterator<T,T&,T*>  Iterator;
    typedef __ListIterator<T,const T&,const T*>  ConstIterator;
    Iterator Begin()
    {
        return _head->_next;
    }

    Iterator End()
    {
        return _head;
    }
    ConstIterator Begin() const
    {
        return _head->_next;
    }

    ConstIterator End() const
    {
        return _head;
    }

    List()
    {
        _head = new Node(T());
        _head->_next = _head;
        _head->_prev = _head;
    }
     List(const List& l)
     {
         _head = new Node(T());
         _head->_next = _head;
         _head->_prev = _head;

         ConstIterator it = l.Begin();
         while(it!=l.End())
         {
             PushBack(*it);
             ++it;
         }
     }

     ~List()
     {
         Clear();

         delete _head;
         _head = NULL;
     }

     void Clear()
     {
            Iterator it = Begin();
            while(it!=End())
            {
                Node* del = it._node;
                ++it;

                delete del;
            }
            _head->_next = _head;
            _head->_prev = _head;
     }

     void PushBack(const T& x)
     {
        Insert(End(),x);
     }

     void PushFront(const T& x)
     {
        Insert(Begin(),x);
     }

     void PopBack()
     {
        Erase(--End());      
     }
     void PopFront()
     {
         Erase(Begin());
     }

     void Insert(Iterator pos,const T& x)
     {
         Node* cur = pos._node;
         Node* prev = cur->_prev;
         Node* tmp = new Node(x);
         prev->_next = tmp;
         tmp->_prev =prev;
         tmp->_next = cur;
         cur->_prev = prev;
     }

     Iterator Erase(Iterator& pos)
     {
       assert(pos!=End());

       Node* prev = (pos._node)->_prev;
       Node* next = (pos._node)->_next;
       prev->_next = next;
       next->_prev = prev;

       delete pos._node;

       pos._node= prev;
       return Iterator(next);

     }

protected:
  Node* _head;
};

void PrintList(const List<int>& l)
{
    List<int>::ConstIterator it = l.Begin();
    while(it!= l.End())
    {
        cout<<*it<<" ";
        ++it;
    }
    cout<<endl;
}

void TestList()
{
    List<int> l;
    l.PushBack(1);
    l.PushBack(2);
    l.PushBack(3);
    l.PushBack(4);
    PrintList(l);

    List<int>:: Iterator it = l.Begin();
    while(it != l.End())
    {
        if(*it%2 == 0)
        {
            l.Erase(it);
        }
        ++it;

    }
    PrintList(l);
}

TestList.cpp

#include<iostream>
using namespace std;

#include"List.h"

int main()
{
    //test_list();
    TestList();
    return 0;
}

三.vector和list的总结与比较。

1.vector为存储的对象分配一块连续的地址空间,因此对vector中的元素随机访问效率很高。在vecotor中插入或者删除某个元素,需要将现有元素进行复制,移动。如果vector中存储的对象很大,或者构造函数复杂,则在对现有元素进行拷贝时开销较大,因为拷贝对象要调用拷贝构造函数。对于简单的小对象,vector的效率优于list。vector在每次扩张容量的时候,将容量扩展2倍,这样对于小对象来说,效率是很高的。
2.list中的对象是离散存储的(循环双向链表),随机访问某个元素需要遍历list。在list中插入元素,尤其是在首尾插入元素,效率很高,只需要改变元素的指针。

综上所述:

vector适用:对象数量变化少,简单对象,随机访问元素频繁
list适用:对象数量变化大,对象复杂,插入和删除频繁
最大的区别是,list是双向的,而vector是单向的。

因此在实际使用时,如何选择这两个容器中哪一个,应根据你的需要而定,一般应遵循下面的原则:
1、如果你需要高效的随即存取,而不在乎插入和删除的效率,使用vector。
2、如果你需要大量的插入和删除,而不关心随即存取,则应使用list。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值