vector 和 list

一、vector和list的使用

(1)vector

基本概念:vector的底层结构和数组类似,拥有一段连续的内存空间,并且起始地址不变。因此可以高效的进行随机随机存取,时间复杂度为o(1);但是因为内存空间是连续的,所以在进行插入和删除操作时,需要移动数据,当数组内存空间不足时,会重新申请一块内存块的拷贝,时间复杂度为o(N).

头文件:

#include<vector>
基本使用:

#include<iostream>
#include<vector>
using namespace std;
void TestVector()
{
    vector<int> v;//创建一个容器
    v.push_back(1);//在顺序表末尾插入数据
    v.push_back(2);
    v.push_back(3);
    v.push_back(4);
    v.push_back(5);
    cout<<v.front()<<endl;//返回第一个元素
    cout<<v.back()<<endl;//返回最后一个元素
    v.insert(v.begin(),10);//插入数据
    v.insert(v.begin(),20);
    v.insert(v.begin(),30);
    v.erase(v.begin());//删除指定位置元素
    //v.assign(2,100);//改变顺序表的size,并重新赋值
    vector<int>::iterator it = v.begin();
    while(it != v.end())
    {
        cout<<*it<<" ";
        ++it;
    }
    cout<<endl;
    cout<<v[2]<<endl;//访问第三个元素
    cout<<v.at(2)<<endl;//访问第三个元素
    v.pop_back();//删除最后一个数据
    cout<<v.size()<<endl;//返回数据个数
    cout<<v.capacity()<<endl;//返回容量
    cout<<v.empty()<<endl;//判断顺序表是否为空
    cout<<v.max_size()<<endl;//求最大的size
    v.clear();//清空数据
    //v.resize(5,200);//开空间并初始化
    v.reserve(10);//不会影响size,只会把容量开够
    cout<<v.capacity()<<endl;
    vector<int>::iterator it1 = v.begin();
    while(it1 != v.end())
    {
        cout<<*it1<<" ";
        ++it1;
    }
    cout<<endl;
}
int main()
{
    TestVector();
      return 0;
}

(2)list

基本概念:list由双向链表实现的,因此空间内存不是连续的。由于list的实现原理,所以随机存取非常没有效率,只能通过指针访问数据,时间复杂度为o(N).由于链表的特点,能高效地进行插入和删除。

头文件:

#include<list>
基本使用:
#include<iostream>
#include<list>
using namespace std;
void TestList()
{
	list<int> l;//创建一个list容器
	l.push_back(1);//在list进行尾插
	l.push_back(2);
	l.push_back(3);
	l.push_back(3);
	l.push_back(4);
	l.push_back(3);
	l.push_front(10);//在list进行头插
	l.push_front(20);
	l.push_front(30);
	l.sort();//排序
	l.unique();//去重,但要求重复的必须有序
	l.erase(l.begin());//删除某个位置
	l.remove(10);//删除指定元素
	list<int> l1;
	l1.push_back(9);
	l1.push_back(8);
	l1.push_back(7);
	l1.sort();
	l.merge(l1);//合并两个有序list
	list<int>::iterator it = l.begin();
	while(it != l.end())
	{
		cout<<*it<<" ";
		++it;
	}
	cout<<endl;
	l.pop_back();//尾删
	l.pop_front();//头删
	l.insert(l.begin(),60);//插入数据
	l.insert(l.end(),60);
	//l.assign(3,100);//改变list的size,并进行初始化
	cout<<l.front()<<endl;//返回第一个元素
	cout<<l.back()<<endl;//返回最后一个元素
	cout<<l.size()<<endl;//返回元素个数
	cout<<l.max_size()<<endl;//返回max_size
	l.clear();//清空list
	l.resize(5,100);//开空间并且进行初始化
	cout<<l.empty()<<endl;//判断list是否为空
}
int main()
{
	TestList();
	return 0;
}

二、模拟实现MyVector

#include<iostream>
#include<assert.h>
using namespace std;

template <class T>
class Vector
{
public:
    typedef T* Iterator;
    typedef const T* ConstIterator;
public:
    Vector()
        :_start(NULL)
        ,_finsh(NULL)
        ,_endOfStorage(NULL)
    {}
    void PushBack(const T& x)
    {
        Iterator end = End();
        Insert(end,x);
    }
    void Insert(Iterator& pos,const T& x)
    {
        size_t n = pos - _start;
        if(_finsh == _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;
        ++_finsh;
    }
    void Erase(Iterator& pos)
    {
        assert(pos);
        for(Iterator start = pos;start != End();++start)
        {
            *start= *(start+1); 
        }
        --_finsh;
    }
    void PopBack()
    {
        if(_start == NULL)
        {
            return;
        }
        --_finsh;
    }
    void Resize(size_t n,const T& val = T())
    {
        if(n<Size())
        {
            _finsh = _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);
    }
    size_t Size()
    {
        return _finsh-_start;
    }
    size_t Capacity()
    {
        return _endOfStorage-_start;
    }
    Iterator End()
    {
        return _finsh;
    }
    Iterator Begin()
    {
        return _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;
            _finsh = _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 _finsh;
    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.Erase(pos);
    v.PopBack();
    v.PopBack();
    cout<<v.Size()<<endl;
    cout<<v.Capacity()<<endl;
    cout<<v[0]<<endl;
    v.Resize(10);
    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;
}
int main()
{
    TestVector();
    return 0;
}

三、模拟实现MyList

#include <iostream>
#include<assert.h>
using namespace std;

template<class T>
struct __ListNode
{
	T _data;
	__ListNode<T>* _prev;
	__ListNode<T>* _next;
	__ListNode<T>(const T& x)
		:_data(x)
		,_next(NULL)
		,_prev(NULL)
	{}
};
template <class T,class Ref,class Ptr>
class __ListIterator 
{	
public:
	typedef __ListNode<T> Node;
	typedef __ListIterator <T,Ref,Ptr> Iterator;
	__ListIterator(Node* node)
		:_node(node)
	{}

	bool operator == (const Iterator& x)
	{
		return _node == x._node;
	}
	bool operator != (const Iterator& x)
	{
		return _node != x._node;
	}
	Iterator operator ++() //前置
	{
		_node = _node->_next;
		return *this;
	}
	Iterator operator ++(int)//后置
	{
		Iterator tmp = *this;
		_node = _node->_next;
		return tmp;
	}
	Iterator operator --()//前置
	{
		_node = _node->_prev;
		return *this;
	}
	Iterator operator --(int)//后置
	{
		Iterator tmp = *this;
		_node = _node->_prev;
		return tmp;
	}
	Ref operator *()//解引用
	{
		return _node->_data;
	}
	Ptr operator ->()//指针
	{
		return &(_node->_data);
	}
	const Ref operator *()const
	{
		return _node->_data;
	}
	const Ptr operator ->()const
	{
		return &(_node->_data);
	}
	Node* _node;
};
template <class T>
class List
{
	typedef __ListNode<T> Node;
public:
	typedef __ListIterator <T,T&,T*> Iterator;
	typedef __ListIterator <T,const T&,const T*> ConstIterator;
	/*Node* GetNode(const T& x)
	{
		return new Node(x);
	}

	List()
	{
		_head = GetNode(T());
		_head->_next = _head;
		_head->_prev = _head;
	}*/
	List()//无参构造函数
	{
		_head = new Node(T());
		_head->_prev = _head;
		_head->_next = _head;
	}
	List(const T& x)//有参构造函数
		:_head(new Node(x))
	{
		_head->_next = _head;
		_head->_prev = _head;
	}
	~List()
	{
		Clear();
		delete _head;
		_head = NULL;
	}
	Iterator Begin()
	{
		return Iterator(_head->_next);
	}
	Iterator End()
	{
		return Iterator(_head);
	}
	ConstIterator Begin()const
	{
		return Iterator(_head->_next);
	}
	ConstIterator End()const
	{
		return Iterator(_head);
	}
	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)
	{
		/*Node* tmp = new Node(x);
		Node* tail = _head->_prev;

		tail->_next = tmp;
		tmp->_prev = tail;

		tmp->_next = _head;
		_head->_prev = tmp;*/
		Insert(End(),x);
	}
	void PushFront(const T& x)
	{
		Insert(Begin(),x);
	}
	//在pos的前面插入
	void Insert(Iterator pos,const T& x)
	{
		Node* cur = pos._node;
		Node* prev = cur->_prev;
		Node* tmp = new Node(x);

		prev->_next = tmp;
		tmp->_next = cur;
		cur->_prev = tmp;
		tmp->_prev = prev;
	}
	void PopFront()
	{
		Erase(Begin());
	}
	void PopBack()
	{
		Erase(--End());
	}
	Iterator Erase(Iterator pos)
//有迭代器失效的问题,所以此处返回要删除的节点的下一个位置的迭代器
	{
		assert(pos != End() && pos._node);
		Node* cur = pos._node;
		Node* prev = cur->_prev;
		Node* next = cur->_next;

		prev->_next = next;
		next->_prev = prev;
		delete cur;

		//pos._node = prev;

		return next;
	}
	Iterator Find(const T& x)
	{
		Iterator it = Begin();
		while(it != End())
		{
			if(*it == x)
			{
				return it;
			}
			++it;
		}
	}
	bool Empty()
	{
		return _head = _head->_next;
	}

protected:
	Node* _head;
};
void Print(List<int>& l)
{
	List<int>::Iterator it = l.Begin();
	while(it != l.End())
	{
		cout<<*it<<" ";
		++it;
	}
	cout<<endl;
}
void TestList()
{
	List<int> l1;
	l1.PushBack(1);
	l1.PushBack(2);
	l1.PushBack(3);
	l1.PushBack(4);
	l1.PushFront(0);
	l1.PushBack(5);
	List<int> ::Iterator it1 = l1.Find(0);
	l1.Erase(it1);
	l1.PopFront();
	l1.PopBack();
	List<int>::Iterator it = l1.Begin();
	while(it != l1.End())
	{
		cout<<*it<<" ";
		++it;
	}
	cout<<endl;
	//List<int>::Iterator it = l1.Begin();
	//while(it != l1.End())
	//{
	//	if(*it % 2 == 0)
	//	{
	//		it = l1.Erase(it);
	//	}
	//	else
	//	{
	//		++it;
	//	}
	//}
	//Print(l1);
}
int main()
{
	TestList();
	return 0;
}




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值