#include<iostream>
#include<stdio.h>
#include<list>
#include<algorithm>

using namespace std;

struct node {
	int a;
	char c;

	bool operator ==(const node&f)
	{
		if (f.a == this->a&&f.c == this->c)
		{
			return true;
		}
		return false;
	}//参数是结构体时需要在结构体内重载"=="(属于隐藏属性)

	bool operator <(const node&f)
	{
		if (f.a > this->a)
		{
			return true;
		}
		return false;
	}//重载运算符"<"
};

void fun(node & d)
{
	cout << d.a << " " << d.c << endl;
}

void Constructor_list()//list_构造
{

	list<node> L(2);
	for_each(L.begin(), L.end(), fun);
	//由于0在ascll码表上的字符是一个不可显示的字符,所以没有显示两列0。强制转换后才会显示。
	cout << " 1.创建空链表" << endl;

	node no ={ 12,'a' };
	list<node>L1(3, no);
	for_each(L1.begin(), L1.end(), fun);
	cout << " 2.为链表赋值并输出n个链表" << endl;

	list<node>L2(L1);
	for_each(L2.begin(), L2.end(), fun);
	cout << " 3.拷贝构造"<< endl;
}

void Construction_list_iterator()//迭代器_构造
{
	list<node>L(2);
	list<node>::iterator ite = L.begin();
	for_each(L.begin(), L.end(), fun);
	cout  << " 1.迭代器构造" << endl;

	list<node>::iterator ite1 = L.begin();
	ite1++;//由于链表是连续的空间, 所以不能进行加法运算, 只能进行自加。
	for (ite1;ite1 != L.end();ite1++)
	{
		cout << ite1->a << " " << ite1->c << endl;
	}
	cout << " 2.利用迭代器输出" << endl;
}

void Attribute_list()//list_属性
{
	list<node>L(3);
	cout << " 1.容量问题不再讨论了,根据设置而定" << endl;

	cout << L.size() <<" 2.输出元素有效个数"<< endl;

	L.resize(5);
	cout <<		L.size() <<" 3.修改容量"<< endl;

	cout << L.empty() << " 4.判空" << endl;

}

void Output_list()//list_输出
{
	node no = { 9,'b' };
	list<node> L(6, no);
	cout << L.back().a << " " << L.back().c << " 1.back()返回尾巴元素"<<endl;

	cout << L.front().a << " " << L.front().c <<" 2.front()返回第一个元素"<< endl;

	cout<<" 3.list不支持下标[]输出"<<endl;
}

void Add_list()//list_添加
{
	list<node> L;
	node no = { 1,'a' };
	L.push_front(no);
	cout << L.front().a << " " << L.back().c << " 1.头添加" << endl;

	node no1 = { 3,'c' };
	L.push_back(no1);
	cout << L.back().a << " " << L.back().c << " 2.尾添加" << endl<<endl;

	list<node>::iterator ite = L.begin();
	ite++;
	node no2 = { 2,'b' };
	L.insert(ite, no2);
	for_each(L.begin(), L.end(), fun);
	cout << " 3.中间插入一个结构体"<<endl;

	node no3 = { 9,'z' };
	L.insert(ite, 2, no3);
	for_each(L.begin(), L.end(), fun);
	cout << " 4.中间插入n个结构体" << endl;

	list<node>L2;
	node no4 = { 6,'v' };
	L2.push_front(no4);
	L2.push_front(no4);
	list<node>::iterator ite1 = L2.begin();
	ite1++;
	L2.insert(ite1, L.begin(), L.end());
	for_each(L2.begin(), L2.end(), fun);
	cout <<" 5.插入另一对象的一段"<< endl;
}

void Delete_list()//list_删除
{
	list<node> L;
	node no = { 1,'a' };
	L.push_front(no);
	L.push_front(no);
	L.push_front(no);
	L.pop_back();
	L.pop_front();
	for_each(L.begin(), L.end(), fun);
	cout << " 1.头/尾删除,pop_(front/back)"<<endl;

	list<node>L1;
	node no1 = { 2,'b' };
	node no2 = { 3,'c' };
	L1.push_front(no2);
	L1.push_front(no1);
	L1.push_front(no);
	list<node>::iterator ite = L1.begin();
	ite++;
	L1.erase(ite);
	for_each(L1.begin(), L1.end(), fun);
	cout << " 2.删除指定元素" << endl;
	//需要注意删除最后一个元素时是L1.erase(--L.end());

	list<node>L3;
	node no3 = { 3,'q' };
	node no4 = { 4,'w' };
	node no5 = { 5,'e' };
	node no6 = { 6,'r' };
	L3.push_front(no6);
	L3.push_front(no5);
	L3.push_front(no4);
	L3.push_front(no3);
	list<node>::iterator ite1 = L3.begin();
	ite1++;
	ite1++;
	L3.erase(ite1, --L3.end());
	for_each(L3.begin(), L3.end(), fun);
	cout << " 3.删除一段" << endl;

	L3.clear();
	for_each(L3.begin(), L3.end(), fun);
	cout << " 4.清除所有" << endl;

	list<node>L4;
	L4.push_front(no6);
	L4.push_front(no5);
	L4.push_front(no5);
	L4.push_front(no3);
	L4.remove(no5);
	for_each(L4.begin(), L4.end(), fun);
	cout << " 5.删除和参数相同的元素" << endl;

	L4.unique();
	for_each(L4.begin(), L4.end(), fun);
	cout << " 6.删除重复元素"<< endl;
}

void 	Modification_list()//list_修改
{
	list<node> L;
	node no = { 1,'a' };
	node no1 = { 2,'b' };
	node no2 = { 3,'c' };
	L.push_front(no2);
	L.push_front(no1);
	L.push_front(no);
	list<node>::iterator ite = L.begin();
	L.assign(ite, L.end());
	for_each(L.begin(), L.end(), fun);
	cout << " 1.删除指定元素" << endl;           //?????????????
}

void Other_list()//list_其他函数
{
	list<node>L;
	list<node>L1;
	list<node>L2;
	node no = { 1,'a' };
	node no1 = { 2,'b' };
	node no2 = { 3,'q' };
	node no4 = { 4,'k' };
	node no5 = { 5,'o' };
	L.push_front(no);
	L1.push_front(no2);
	L1.push_front(no1);
	L2.push_front(no5);
	L2.push_front(no4);
	L.swap(L1);
	for_each(L.begin(), L.end(), fun);
	cout << " 1.swap()交换函数" << endl;

	L.reverse();
	for_each(L.begin(), L.end(), fun);
	cout << " 2.reverse()翻转函数" << endl;

	L.sort();
	for_each(L.begin(), L.end(), fun);
	cout << " 3.sort()函数(此处为list成员函数)" << endl;
	cout << " 4.从大到小排序:①修改重载里的>号 ②配合reverse()" << endl;

	L2.merge(L);
	for_each(L2.begin(), L2.end(), fun);
	cout << " 5.合并两个有序链表(默认升序,可在<重载中修改为降序)" << endl;

	list<node>::iterator ite = L.begin(); 
	list<node>::iterator ite1 = L2.begin();
	L.splice(ite, L2,L2.begin()++,--L2.end());
	for_each(L.begin(), L.end(), fun);
	cout << " 6.splice()链表拼接" << endl;

	list<node>::iterator ite3 = find(L.begin(), L.end(), no2);
	cout << ite3->a << " " << ite3->c << " 7.find()查找函数"<<endl;
}


void Print()
{
	cout << "------------------------------------------------------------------------------------" << endl << endl;
}

int main()
{
	Print();
	Constructor_list();//list_构造
	Print();
	Construction_list_iterator();//迭代器_构造
	Print();
	Attribute_list();//list_属性
	Print();
	Output_list();//list_输出
	Print();
	Add_list();//list_添加
	Print();
	Delete_list();//list_删除
	Print();
	Modification_list();//list_修改
	Print();
	Other_list();//list_其他函数
	system("pause");
	return 0;
}

测试结果:

数组和链表的区别:

vctor:
① 随机访问(下标运算)快;
② 尾添加,不申请空间的情况下非常快
③ 不支持快速插入和删除,比较慢
List:
① 随机访问慢,也支持下标。
② 支持快速插入和删除。

list的底层是双向链表;forward_list的底层是单向链表。