STL之list

List原理

      首先呢,我们得说list是一个泛化的双向链表,支持各种频繁的插入删除操作,每次插入一个新的节点,都会动态分配一块对应大小的内存块,每次删除一个新的节点都会

release该对应内存大小,也就是说list对应内存当中是离散的。也就是说list是不支持随机访问的,自然也就没有再分配(realloc操作),自然也就没有可获得再分配之前的

capacity属性了,list对象,在没有指定元素个数之前,是不会分配内存的。

与我们所知的不太相符的是list不仅仅是一个双向链表,而且是一个环状双向链表,换句话说,list就是首尾相连的,要知道的是list链表的头结点是不存储任何数据的,她就是头结点,与我们第一次insert的节点并不是一个哦。。插入第一个节点之后,才进行扩展,总而言之,list双向环状链表就是首尾相连的啊

#include "stdafx.h"

#include<list>
#include<iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	//list 容器初始化为4个元素大小,同时每个里面默认值都是5
	list<int>m_hinstance(4,5);
	//开始遍历查看是否是这样的
	list<int>::iterator itr;
	cout<<"第一次遍历结果\n";
	for(itr=m_hinstance.begin();itr!=m_hinstance.end();itr++)
	{
		cout<<*itr<<endl;
	}
	//list的增加元素操作
	cout<<"在末尾增加一个元素1之后的遍历";	
	m_hinstance.push_back(1);
	for(itr=m_hinstance.begin();itr!=m_hinstance.end();itr++)
	{
		cout<<*itr<<endl;
	}
	return 0;
}

这样的话,push_back是不是就不对了?不明白...

#include "stdafx.h"

#include<list>
#include<iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	//list 容器初始化为4个元素大小,同时每个里面默认值都是5
	list<int>m_hinstance(4,5);
	//开始遍历查看是否是这样的
	list<int>::iterator itr;
	cout<<"第一次遍历结果\n";
	for(itr=m_hinstance.begin();itr!=m_hinstance.end();itr++)
	{
		cout<<*itr<<endl;
	}
	//list的增加元素操作
	cout<<"在首部增加一个元素1之后的遍历";	
	m_hinstance.push_front(1);
	for(itr=m_hinstance.begin();itr!=m_hinstance.end();itr++)
	{
		cout<<*itr<<endl;
	}
	return 0;
}



是不是发现push_front没有效果??

可如果在push_front之后再加一句push_front就可以了!!!

// LIst.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include<list>
#include<iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	//list 容器初始化为4个元素大小,同时每个里面默认值都是5
	list<int>m_hinstance(4,5);
	//开始遍历查看是否是这样的
	list<int>::iterator itr;
	cout<<"第一次遍历结果\n";
	for(itr=m_hinstance.begin();itr!=m_hinstance.end();itr++)
	{
		cout<<*itr<<endl;
	}
	//删除第一一个元素
	itr=m_hinstance.begin();
	m_hinstance.erase(itr);//如果是有remove,则是删除其值是参数的所有元素
	cout<<"删除第一个元素之后的遍历\n";
	for(itr=m_hinstance.begin();itr!=m_hinstance.end();itr++)
	{
		cout<<*itr<<endl;
	}
	
	return 0;
}

删除元素还是没有问题的哈,只是查找,我就不说了,因为list在内存中是离散分布的不可能支持随机访问,其效率也是最低的,在这里除了遍历一遍我也没有别的方法了,在这就不说了

话说,list的push操作是不是还有问题?能力有限,在这儿就不多说了...

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

世纪殇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值