list
介绍
功能:将数据进行链式存储
- list模板类在list头文件中使用,表示双向链表。除了第一个和最后一个元素外,每个元素都与前后的元素想链接,意味着可以双向遍历链表。
- 由于链表的存储方式并不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器
- 除序列和可反转容器的函数外,list模板类还包含了链接表专用的成员函数。
- 采用动态存储分配,不会造成内存浪费和溢出.
- 链表执行插入和删除操作十分方便,修改指针即可,不需要移动大量元素.。
- 链表灵活,但是空间(指针域) 和 时间(遍历)额外耗费较大;List有一个重要的性质,插入操作和删除操作都不会造成原有list迭代器的失效,这在vector是不成立的
list与vector比较
- list和vector之间关键区别在于,list在链表中任一位置进行插入和删除的时间都是固定的(vector模板提供了除结尾的线性时间的插入和删除,在结尾处,它提供了固定时间的插入和删除)。
- vector强调随机访问快速访问,而list强调的是元素的快速插入和删除。
- 与vector相似,list也是可反转容器,与vector不同的是,list不支持数组表示法和随机访问。
- 与矢量迭代器不同,从容器中插入或删除元素之后,链表迭代器指向将不变。(解释:假设有个指向vector容器第五个元素的迭代器,并在容器的起始插入一个元素,但必须移动其他所有元素,以便腾出位置,插入后,第五个元素包含的值将是以前第四个元素的值。因此,迭代器的位置不变,但数据不同,然后在链表中插入新元素并不会移动已有的元素,只是修改链接信息,指向某个元素的迭代器仍然指向该元素,但链接的元素与以前不同)。
list成员函数
下面程序演示这些方法和insert()方法的用法:
#include <iostream>
#include<list>
#include<iterator>
#include<algorithm>
using namespace std;
void outint(int n)
{
cout<<n<<" ";
}
int main()
{
list<int> l1(5,2);
int stuff[5]={1,2,4,8,6};
list<int> l2;
l2.insert(l2.begin(),stuff,stuff+5);
int more[6]={6,4,2,4,6,5};
list<int> l3(l2);
l3.insert(l3.end(),more,more+6);
cout<<"List l1:";
for_each(l1.begin(),l1.end(),outint);
cout<<endl<<"List l2:";
for_each(l2.begin(),l2.end(),outint);
cout<<endl<<"List l3:";
for_each(l3.begin(),l3.end(),outint);
l3.remove(2);
cout<<endl<<"List l3 minus 2s: ";
for_each(l3.begin(),l3.end(),outint);
l3.splice(l3.begin(),l1);
cout<<endl<<"Lsit l3 after splice:";
for_each(l3.begin(),l3.end(),outint);
cout<<endl<<"List one:";
for_each(l1.begin(),l1.end(),outint);
l3.unique();
cout<<endl<<"List l3 after unique: ";
for_each(l3.begin(),l3.end(),outint);
l3.sort();
l3.unique();
cout<<endl<<"List l3 after sort & unique: ";
for_each(l3.begin(),l3.end(),outint);
l2.sort();
l3.merge(l2);
cout<<endl<<"Sorted two merged into l3: ";
for_each(l3.begin(),l3.end(),outint);
cout<<endl;
return 0;
}
注:程序使用了for_each()算法和outint()函数来显示列表。在c++11中,也可以使用for循环:
for(auto x : l3)cout<<x<<" ";
- insert()和splice()之间的区别在于:insert()将原始区间的副本插入到目标地址,而splice()则将原始区间移动目标地址。因此,在l1的内容与l3合并后,l1为空。(splice()方法还有其他原型,用于移动单个元素和元素之间)。splice()方法执行后,迭代器仍然有效。(指向l1,重新定位到l3后,迭代器仍然指向相同的元素)。
- unique()只能将相连的相同值压缩为单个值。程序执行l3.unique()后,l3中仍包含不相邻的两个4和两个6。但应用sort()后再应用unique()时,每个值将占一个位置。
- 非sort()函数,但需要随机访问迭代器。不能将用与链表。
list构造函数
- list lst; //list采用采用模板类实现,对象的默认构造形式:
- list(beg,end); //构造函数将[beg, end)区间中的元素拷贝给本身。
- list(n,elem); //构造函数将n个elem拷贝给本身。
- list(const list &lst); //拷贝构造函数。
#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.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
printList(L1);
list<int>L2(L1.begin(),L1.end());
printList(L2);
list<int>L3(L2);
printList(L3);
list<int>L4(10, 1000);
printList(L4);
}
int main()
{
test01();
return 0;
}
list 赋值和交换
- assign(beg, end); //将[beg, end)区间中的数据拷贝赋值给本身
- assign(n, elem); //将n个elem拷贝赋值给本身
- list& operator=(const list &lst); //重载等号操作符
- swap(lst); //将lst与本身的元素互换。
#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.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
printList(L1);
list<int>L2;
L2 = L1;
printList(L2);
list<int>L3;
L3.assign(L2.begin(), L2.end());
printList(L3);
list<int>L4;
L4.assign(10, 100);
printList(L4);
}
//交换
void test02()
{
list<int>L1;
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
list<int>L2;
L2.assign(10, 100);
cout << "交换前: " << endl;
printList(L1);
printList(L2);
cout << endl;
L1.swap(L2);
cout<<"交换后: "<<endl;
printList(L1);
printList(L2);
}
int main()
{
//test01();
test02();
return 0;
}
list 大小操作
- size(); //返回容器中元素的个数
- empty(); //判断容器是否为空
- resize(num); //重新指定容器的长度为num,若容器变长,则以默认值填充新位置;//如果容器变短,则末尾超出容器长度的元素被删除
- resize(num, elem); //重新指定容器的长度为num,若容器变长,则以elem值填充新位置。//如果容器变短,则末尾超出容器长度的元素被删除。
#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.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
if(L1.empty())
{
cout<<"L1为空"<<endl;
}else
{
cout<<"L1不为空"<<endl;
cout<<"L1的大小为:"<<L1.size()<<endl;
}
//重新指定大小
L1.resize(10);
printList(L1);
L1.resize(2);
printList(L1);
}
int main()
{
test01();
return 0;
}
总结:
- 判断是否为空 — empty
- 返回元素个数 — size
- 重新指定个数 — resize
list 插入和删除
- push_back(elem);//在容器尾部加入一个元素
- pop_back();//删除容器中最后一个元素
- push_front(elem);//在容器开头插入一个元素
- pop_front();//从容器开头移除第一个元素
- insert(pos,elem);//在pos位置插elem元素的拷贝,返回新数据的位置
- insert(pos,n,elem);//在pos位置插入n个elem数据,无返回值。
- insert(pos,beg,end);//在pos位置插入[beg,end)区间的数据,无返回值。
- clear();//移除容器的所有数据
- erase(beg,end);//删除[beg,end)区间的数据,返回下一个数据的位置
- erase(pos);//删除pos位置的数据,返回下一个数据的位置。
- remove(elem);//删除容器中所有与elem值匹配的元素。
#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> L;
//尾插
L.push_back(10);
L.push_back(20);
L.push_back(30);
//头插
L.push_front(100);
L.push_front(200);
L.push_front(300);
printList(L);
//尾删
L.pop_back();
printList(L);
//头删
L.pop_front();
printList(L);
//插入
list<int>::iterator it = L.begin();
L.insert(++it, 1000);
printList(L);
//删除
it = L.begin();
L.erase(++it);
printList(L);
//移除
L.push_back(10000);
L.push_back(10000);
L.push_back(10000);
printList(L);
L.remove(10000);
printList(L);
//清空
L.clear();
printList(L);
}
int main()
{
test01();
return 0;
}
总结:
- 尾插 — push_back
- 尾删 — pop_back
- 头插 — push_front
- 头删 — pop_front
- 插入 — insert
- 删除 — erase
- 移除 — remove
- 清空 — clear
list 数据存取
函数原型:
- front(); //返回第一个元素。
- back(); //返回最后一个元素。
#include<iostream>
#include<list>
using namespace std;
void printList(list<int> &L1)
{
for(list<int>::iterator it=L1.begin();it!=L1.end();it++)
{
cout<<*it<<" ";
}
cout<<endl;
}
void test01()
{
list<int>L1;
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
//cout << L1.at(0) << endl;//错误 不支持at访问数据
//cout << L1[0] << endl; //错误 不支持[]方式访问数据
cout << "第一个元素为: " << L1.front() << endl;
cout << "最后一个元素为: " << L1.back() << endl;
//list容器的迭代器是双向迭代器,不支持随机访问
list<int>::iterator it = L1.begin();
//it = it + 1;//错误,不可以跳跃访问,即使是+1
}
int main()
{
test01();
return 0;
}
总结:
- list容器中不可以通过[]或者at方式访问数据
- 返回第一个元素 — front
- 返回最后一个元素 — back
list 反转和排序
- reverse(); //反转链表
- sort(); //链表排序
#include<iostream>
#include<list>
using namespace std;
void printList(list<int> &L1)
{
for(list<int>::iterator it=L1.begin();it!=L1.end();it++)
{
cout<<*it<<" ";
}
cout<<endl;
}
//成员函数重载 <运算符
bool myCompare(int val1,int val2)
{
return val1>val2;
}
void test01()
{
list<int>L1;
L1.push_back(90);
L1.push_back(20);
L1.push_back(30);
L1.push_back(70);
//反转容器的元素
L1.reverse(); printList(L1);
//排序 L.sort();
//默认的排序规则 从小到大
printList(L1);
L1.sort(myCompare);
//指定规则,从大到小
printList(L1);
}
int main()
{
test01();
return 0;
}
总结:
- 反转 — reverse
- 排序 — sort (成员函数)
排序案例
案例描述:将Person自定义数据类型进行排序,Person中属性有姓名、年龄、身高
排序规则:按照年龄进行升序,如果年龄相同按照身高进行降序
#include<iostream>
#include<list>
#include<algorithm>
#include<string>
using namespace std;
class Person
{
public: Person(string name, int age , int height)
{
m_Name = name;
m_Age = age;
m_Height = height;
}
public:
string m_Name;//姓名
int m_Age;//年龄
int m_Height; //身高
};
bool ComparePerson(Person& p1, Person& p2)
{ if (p1.m_Age == p2.m_Age)
{
return p1.m_Height > p2.m_Height;
}else
{ return p1.m_Age < p2.m_Age;
}
}
void test01()
{
list<Person> L;
Person p1("小明", 13 , 175);
Person p2("小华", 12 , 180);
Person p3("小黑", 17 , 170);
Person p4("Tom", 15 , 190);
Person p5("lucy", 14 , 160);
Person p6("lili", 18 , 200);
L.push_back(p1);
L.push_back(p2);
L.push_back(p3);
L.push_back(p4);
L.push_back(p5);
L.push_back(p6);
for (list<Person>::iterator it = L.begin(); it != L.end(); it++) {
cout << "姓名: " << it->m_Name << " 年龄: " << it->m_Age
<< " 身高: " << it->m_Height << endl;
}
cout << "---------------------------------" << endl;
L.sort(ComparePerson); //排序
for (list<Person>::iterator it = L.begin(); it != L.end(); it++) {
cout << "姓名: " << it->m_Name << " 年龄: " << it->m_Age
<< " 身高: " << it->m_Height << endl;
}
}
int main()
{
test01();
return 0;
}
list存放自定义数据 如果删除某个节点 必 须重载 ==运算符
#include<iostream>
#include<list>
#include<algorithm>
#include<string>
using namespace std;
class Person
{
public:
string name;
int age;
public:
Person(string name,int age)
{
this->name=name;
this->age=age;
}
//成员函数重载 == 运算符
bool operator ==(const Person &ob)
{
if(this->name == ob.name && this->age == ob.age)
return true;
return false;
}
};
void printListPerson(list<Person> &L)
{
cout<<"-----"<<endl;
for(list<Person>::iterator it=L.begin();it!=L.end();it++)
{
cout<<(*it).name<<" "<<(*it).age<<endl;
}
}
void test01()
{
//存放自定义数据
list<Person> L;
L.push_back(Person("小明",8));
L.push_back(Person("小莫", 28));
L.push_back(Person("小华", 18));
L.push_back(Person("Tom", 19));
printListPerson(L);
//删除小华
Person tmp("循环", 18);
//重载==运算符
L.remove(tmp);
printListPerson(L);
}
int main()
{
test01();
return 0;
}
forward_list(c++11)
c++11新增了容器类forward_list,它实现了单链表,每个节点只连接到下一个节点,而没有链接到前一个节点。因此forward_list只需要正向迭代器,而不需双向迭代器。因此,不同于vector和list,forward_list是不可反转的容器。相比lsit,forward_list更简单,更紧凑,但功能更少。