#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
//底层结构是双向链表
struct Node{
int a;
char c;
};
struct Node1{ //重点中的重点
int a;
char c;
Node1(int d,char e)
{
a=d;
c=e;
}
bool operator==(const Node1& i)
{
if(i.a==this->a&&i.c==this->c)
{
return true;
}
return false;
}
bool operator<(const Node1& f)
{
if(this->a<f.a) //if(this->a>f.a) //可以改变> , <
return true;
return false;
}
};
void fun(Node d)
{
cout << d.a << " " << d.c << "\n";
}
void fun1(Node1 d)
{
cout << d.a << " " << d.c << "\n";
}
void listdefine()
{
list<Node> ls;
list<Node> l(5);
Node no={12,'a'};
list<Node> l1(5,no);
list<Node> l2(l1);
list<Node> l3(l2.begin(),l2.end());
list<Node>::iterator ite=l3.begin(); //不可以加数,只可以自加,
ite++; //ite+4; 错误
list<Node>l4(ite,l3.end());
//for_each(l3.begin(),l3.end(),fun);
for_each(l4.begin(),l4.end(),fun);
}
void listsize()
{
Node no={12,'a'};
list<Node> ls(6,no);
cout << ls.size() << endl;
//for_each(ls.begin(),ls.end(),fun);
ls.resize(18); //新扩空间全为0
cout << ls.size() << endl;
for_each(ls.begin(),ls.end(),fun);
ls.resize(3);
cout << ls.size() << endl;
//cout << ls.empty() << endl;
ls.resize(0);
//cout << ls.empty() << endl;
}
void list_put_increase()
{
//Node no={12,'a'};
//list<Node> ls(5,no);
//输出:循环,迭代器,for_each(不支持下标运算
/*
for(list<Node>::iterator ite=ls.begin();ite!=ls.end();ite++)
{
cout << ite->a << " " << ite->c << endl;
}
*/
//cout << ls.back().a << " " << ls.back().c << endl;
//cout << ls.front().a << " " << ls.front().c << endl;
//list<Node> l1;
//ls.push_front(no);
//for_each(ls.begin(),ls.end(),fun);
list<Node1> li;
li.push_front(Node1(12,'a')); //头添加
li.push_back(Node1(13,'b')); //尾添加
list<Node1>::iterator ite=li.begin();
ite++;
li.insert(ite,Node1(15,'c'));
ite--;
li.insert(ite,3,Node1(16,'d'));
cout << ite->a << endl;
for_each(li.begin(),li.end(),fun1);
}
void list_delete_modification()//
{
list<Node1> ls;
ls.push_front(Node1(12,'a'));
ls.push_back(Node1(13,'b'));
list<Node1>::iterator ite=ls.begin();
ls.insert(++ite,3,Node1(14,'c'));
for_each(ls.begin(),ls.end(),fun1);
//ls.pop_back(); //尾删除
//ls.pop_front(); //头删除
//ls.erase(ls.begin(),--ls.end()); //选位删除
//ls.clear(); //清空
//ls.unique(); //去重
//ls.assign(3,Node1(11,'x')); //重新赋值
ls.begin()->a=(--ls.end())->a; //改
//还可以用迭代器
for_each(ls.begin(),ls.end(),fun1);
}
void list_merge()
{
list<Node1> ls;
ls.push_front(Node1(12,'a'));
ls.push_back(Node1(13,'b'));
ls.insert(++ls.begin(),3,Node1(14,'c'));
list<Node1> ls1;
ls1.push_front(Node1(1,'x'));
ls1.push_front(Node1(25,'y'));
ls1.push_back(Node1(3,'z'));
ls.merge(ls1); //合并
//ls1.swap(ls); //交换
//ls.reverse(); //倒序 //reserve
//ls.sort(); //从小到大
//ls.splice(); //拼接
list<Node1>::iterator ite=find(ls.begin(),ls.end(),Node1(12,'a')); //find需要重载==号 Node1(12,'f')也不影响
cout << ite->a << " " << ite->c << endl;
for_each(ls.begin(),ls.end(),fun1);
}
int main()
{
//listdefine();
//listsize();
//list_put_increase();
//list_delete_modification();
//list_merge();
return 0;
}
/*
与vector的区别:
随机访问慢,也支持下标,快速插入删除
forward_list容器,2011年以后的编译器可用,更快,无size函数
*/
来源:https://www.cnblogs.com/sos3210/p/12292373.html