C++ STL标准库:序列容器 双向链表list

C++ STL标准库系列文章:

[STL] 1.简介
[STL] 2.序列容器 固定数组array(C++ 11)
[STL] 3.序列容器 动态数组vector
[STL] 4.序列容器 双端队列deque
[STL] 5.序列容器 双向链表list
[STL] 6.序列容器 单向链表forward_list(C++ 11)
[STL] 7.适配器简介
[STL] 8.容器适配器 栈stack
[STL] 9.容器适配器 队列queue
[STL] 10.容器适配器 优先队列priority_queue
[STL] 11.关联容器 集合set
[STL] 12.关联容器 映射map
[STL] 13.关联容器 多重集合multiset
[STL] 14.关联容器 多重映射multimap
[STL] 15.关联容器 无序集合unordered_set(C++ 11)
[STL] 16.关联容器 无序集合unordered_map(C++ 11)
[STL] 17.仿函数functor与函数对象
[STL] 18.预定义函数对象、仿函数适配器
[STL] 19.算法algorithm
[STL] 20.迭代器适配器
[STL] 21.空间配置器allocator

list - C++ Reference (cplusplus.com)

1. 简介

list(双向链表),不支持随机访问,对任何位置进行 高效插入、删除的序列容器。

list类似于数据结构的 双向链表;通过链表指针串连成逻辑意义上的线性表,因此它的内存空间 不连续

头文件:

#include<list>
using namespace std;

2. 基本使用

#include<iostream>

#include<list>
using namespace std;

int main()
{
   list<int> l;//空的双向链表
   cout << "元素个数:" << l.size() << endl;

   list<int> l2(5);//初始化5个元素,默认值为类型的默认值
   cout << "元素个数:" << l2.size() <<"  "<<  * (l2.begin() )<< endl;

   list<int> l3(5,111);//初始化5个元素,每个元素初始值为111
   cout << "元素个数:" << l3.size() << "  " << *(l3.begin()) << endl;

   list<int> l4( l3 );//拷贝构造
   cout << "元素个数:" << l4.size() << "  " << *(l4.begin()) << endl;

   //不支持[]运算符,因为效率低
   //cout << l4[0] << endl;

   //验证了list容器的内存空间是不连续的
   for (list<int>::iterator   it = l3.begin(); it !=l3.end(); it++)
   {
   	cout << &(*it) << "   ";
   }
   cout << endl;

   return 0;
}

2. 迭代器使用

#include<iostream>

#include<list>
using namespace std;

template<class T>
void   Print(T  begin, T  end)
{
   //此处编写代码
   for (T p = begin; p != end; ++p)
   {
   	cout << *p << "		";
   }
   cout << endl;
}

int main()
{ 
   list<int> l3(5, 111);//初始化5个元素,每个元素初始值为111
   cout << "元素个数:" << l3.size() << "  " << *(l3.begin()) << endl;

   //验证list容器的迭代器类型(5种之一)
   //双向迭代器bidirectional_iterator_tag
   cout << typeid(list<int>::iterator::iterator_category).name() << endl;

   //双向迭代器比随机访问迭代器弱一些,支持  ++  --   !=  ==  =  *   不支持[]  +n -n  +=n  -=n
   list<int>::iterator  it = l3.begin(); //指向容器l3的第一个元素
   cout << *it << endl;

   *(++it) =  222;
   *(++it) = 333;
   *(++it) = 444;
   *(++it) = 555;

   ++it;//指向最后一个元素的下一个

   cout <<"迭代器指向末尾的下一个"<< (it == l3.end() )<< endl;

   --it;//指向最后一个元素
   cout << *it << endl;

   //it += 3;//不支持
   //it + 3;//不支持
   //it[0];//不支持

   //const_iterator常迭代器,类似于 const  int  *
   list<int>::const_iterator  it2 = l3.cbegin();
   //*it2 = 1;//不能修改常迭代器指向的内容

   //正向遍历
   for (list<int>::iterator it = l3.begin(); it != l3.end(); it++)
   {
   	cout <<  *it << "   ";
   }
   cout << endl;

   //反向遍历
   for (list<int>::reverse_iterator it = l3.rbegin(); it != l3.rend(); it++)
   {
   	cout << *it << "   ";
   }
   cout << endl;

   //测试自己的算法(迭代器带来的好处,算法无需关系容器的具体内存结构,就可以遍历)
   Print<list<int>::iterator>(l3.begin(), l3.end());
   Print(l3.crbegin(), l3.crend());//自己推到迭代器类型

   return 0;
}

3. 增加、删除、插入元素

#include<iostream>

#include<list>
using namespace std;

int main()
{
   list<int>  l;

   //头部插入一个节点(list容器肯定知道头部的位置)
   l.push_front(111);

   //尾部插入一个节点(list容器肯定知道尾部的位置)
   l.push_back(444);
   l.push_back(555);

   //在某个迭代器的位置之前插入
   l.insert(l.begin(), 222);

   //在某个迭代器的位置之前插入n个相同值元素
   l.insert(l.begin(), 3,333);

   //访问链表第一个元素
   l.front() = 1;
   cout <<"第一个元素:"<< l.front() << endl; 

   //访问链表最后一个元素
   cout << "最后一个元素:" << l.back() << endl;

   for (list<int>::iterator it = l.begin();  it!= l.end(); it++)
   {
   	cout << *it << "	   ";
   }
   cout << endl;

   //删除链表头的元素
   l.pop_front();

   //删除链表尾的元素
   l.pop_back();

   //删除某个迭代器指向的元素
   l.erase(l.begin());
    
    //删除一段迭代器区间
   l.erase(l.begin(),l.end());

   //清空链表
   l.clear();

   for (list<int>::iterator it = l.begin(); it != l.end(); it++)
   {
   	cout << *it << "	   ";
   }
   cout << endl;

   return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

超级D洋葱

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

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

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

打赏作者

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

抵扣说明:

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

余额充值