#include<iostream>
#include<list>
using namespace std;
//双向循环链表容器
//迭代器只支持前移一位和后移一位,不能跳着访问
//动态存储分配,不会造成内存浪费,删除插入方便只需改动指针
//空间和时间耗费较大
//打印函数
void printList(list<int>&L)
{
for (list<int>::iterator it = L.begin(); it != L.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//构造函数
void test01()
{
//默认构造
list<int>L1;
L1 = { 0,1,2,3,4,5 };
//区间构造
list<int>L2(L1.begin(), L1.end());
//n个elem
list<int>L3(5, 10);
//拷贝构造
list<int>L4(L3);
}
//赋值和交换
void test02()
{
list<int>L1 = { 0,1,2,3,4,5 };
//区间赋值
list<int>L2;
L2.assign(L1.begin(), L1.end());
//n个elem赋值
list<int>L3;
L3.assign(5, 10);
//重载=运算符
list<int>L4;
L4 = L3;
//交换
L4.swap(L2);
}
//大小操作
void test03()
{
list<int>L1;
//元素个数
L1.size();
//是否为空
L1.empty();
//重新指定大小,变长填充默认值
L1.resize(10);
//重新指定大小,变长则填充elem,
L1.resize(10, 5);
}
//插入和删除
void test04()
{
list<int>L1;
//尾部加元素
L1.push_back(5);
//删除最后一个元素
L1.pop_back();
//开头插入元素
L1.push_front(10);
//移除第一个元素
L1.pop_front();
//在指定位置插入元素,返回新数据的位置
L1.insert(L1.begin(), 5);
//在指定位置插入n个元素,无返回值
L1.insert(L1.end(), 3, 1);
//清除所有元素
L1.clear();
//删除区间内元素,返回下一数据位置
L1.erase(L1.begin(), L1.end());
//删除指定位置元素,返回下一数据位置
L1.erase(L1.begin());
//删除容器内所有elem
L1.remove(10);
}
//数据存取
void test05()
{
list<int>L1;
//返回第一个元素
L1.front();
//返回最后一个元素
L1.back();
}
//制定排序规则
bool sortRule(int L1,int L2)
{
return L1 > L2;
}
//反转和排序
void test06()
{
list<int>L1 = { 45,5,68,41,23 };
//反转链表
L1.reverse();
printList(L1);
//排序
L1.sort();
printList(L1);
//加上排序规则后
L1.sort(sortRule);
printList(L1);
}
int main()
{
test06();
system("pause");
return 0;
}