1、list基本概念
功能:将数据进行链式存储
链表是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接实现的
链表的组成:链表由一系列结点组成
结点的组成:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域
链表的优点:
- 可以对任意的位置进行快速插入或删除元素,修改指针即可,不需要移动大量元素
- 采用动态存储分配,不会造成内存的浪费和溢出
缺点:
- 容器遍历速度没有数组快,占用空间 比 数组 大
STL中的链表是一个双向循环链表
list有一个重要性质,插入操作和删除操作都不会造成原有list迭代器的失效,这在vector是不成立的。
总结:STL中list和vector是两个最常用的容器,各有优缺点
2、list构造函数
功能:创建list容器
函数原型:
- list<T> lst;list采用模板类实现,对象的默认构造形式
- list(beg,end);构造函数将beg,end区间内的元素拷贝给本身
- list(n,elem);构造函数将n个elem拷贝给本身
- list(const list &lst);拷贝构造函数
#include<list>
using namespace std;
void printList(list<int> &l)
{
for (list<int>::iterator lit = l.begin(); lit != l.end(); lit++)
{
cout << *lit << " ";
}
cout << endl;
}
void test01()
{
list<int> l1;
l1.push_back(10);
l1.push_back(20);
l1.push_back(30);
l1.push_back(40);
l1.push_back(50);
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();
system("pause");
return 0;
}
10 20 30 40 50
10 20 30 40 50
10 20 30 40 50
1000 1000 1000 1000 1000 1000 1000 1000 1000 1000
请按任意键继续. . .
3、list赋值和交换
功能:给list容器进行赋值,交换list容器
函数原型:
- assign(beg,end);将beg,end区间中的数据拷贝给本身
- assign(n,elem);将n个elem拷贝赋值给本身
- list& operator=(const list &lst);重载等号运算符
- swap(lst);交换lst和本身的元素
#include<list>
using namespace std;
void printList(list<int> &l)
{
for (list<int>::iterator lit = l.begin(); lit != l.end(); lit++)
{
cout << *lit << " ";
}
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(l1.begin(), l1.end());
printList(l3);
list<int> l4;
l4.assign(10, 12);
printList(l4);
l4.swap(l2);
printList(l2);
printList(l4);
}
int main()
{
test01();
system("pause");
return 0;
}
4、list大小操作
功能:对list容器大小进行操作
函数原型:
-
empty();判断容器是否为空
-
size();返回容器中元素的个数
-
resize(int num);重新指定容器的长度为num,若容器变长,则以默认值填充新位置,若容器变短,则末尾超出容器长度的元素被删除
-
resize(int num, elem);重新指定容器的长度为num,若容器变长,则以elem填充新位置,若容器变短,则末尾超出容器长度的元素被删除
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cmath>
#include<fstream>
#include<algorithm>//标准算法头文件
#include<deque>
#include<stack>
#include<list>
using namespace std;
void printList(list<int> &l)
{
for (list<int>::iterator lit = l.begin(); lit != l.end(); lit++)
{
cout << *lit << " ";
}
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);
if (l1.empty())
{
cout << "null" << endl;
}
else
{
cout << "有元素" << endl;
}
cout << l1.size() << endl;
l1.resize(10);
printList(l1);
l1.resize(20, 100);
printList(l1);
l1.resize(15);
printList(l1);
}
int main()
{
test01();
system("pause");
return 0;
}
10 20 30 40
有元素
4
10 20 30 40 0 0 0 0 0 0
10 20 30 40 0 0 0 0 0 0 100 100 100 100 100 100 100 100 100 100
10 20 30 40 0 0 0 0 0 0 100 100 100 100 100
请按任意键继续. . .
5、list插入与删除
功能:对list容器进行数据插入与删除
函数原型:
- push_back(elem);尾部插入elem
- pop_back();尾部删除
- push_front(elem);头部插入elem
- pop_front();头部删除
- insert(pos迭代器,elem);在pos处插入elsm,返回新数据的位置
- 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 <string>
#include <vector>
#include <ctime>
#include <cmath>
#include<fstream>
#include<algorithm>//标准算法头文件
#include<deque>
#include<stack>
#include<list>
using namespace std;
void printList(list<int> &l)
{
for (list<int>::iterator lit = l.begin(); lit != l.end(); lit++)
{
cout << *lit << " ";
}
cout << endl;
}
void test01()
{
list<int> l1;
l1.push_back(10);
l1.push_back(20);
l1.push_back(30);
l1.push_back(40);
l1.push_front(1);
l1.push_front(2);
l1.push_front(3);
l1.push_front(4);
printList(l1);
l1.pop_back();
l1.pop_front();
printList(l1);
l1.insert(l1.begin(), 1000);
l1.insert(l1.end(), 2, 100);
printList(l1);
l1.erase(l1.begin());
printList(l1);
l1.remove(100);
printList(l1);
}
int main()
{
test01();
system("pause");
return 0;
}
4 3 2 1 10 20 30 40
3 2 1 10 20 30
1000 3 2 1 10 20 30 100 100
3 2 1 10 20 30 100 100
3 2 1 10 20 30
请按任意键继续. . .
6、list数据存取
函数原型:
- front();返回第一个元素
- back();返回最后一个元素
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cmath>
#include<fstream>
#include<algorithm>//标准算法头文件
#include<deque>
#include<stack>
#include<list>
using namespace std;
void printList(list<int> &l)
{
for (list<int>::iterator lit = l.begin(); lit != l.end(); lit++)
{
cout << *lit << " ";
}
cout << endl;
}
void test01()
{
list<int> l1;
l1.push_back(10);
l1.push_back(20);
l1.push_back(30);
l1.push_back(40);
l1.push_front(1);
l1.push_front(2);
l1.push_front(3);
l1.push_front(4);
printList(l1);
cout << l1.front() << endl;
cout << l1.back() << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
4 3 2 1 10 20 30 40
4
40
请按任意键继续. . .
7、list反转和排序
功能:将容器中的元素反转,以及将容器中的数据进行排序
函数原型:
- reverse();反转链表
- sort();链表排序
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cmath>
#include<fstream>
#include<algorithm>//标准算法头文件
#include<deque>
#include<stack>
#include<list>
using namespace std;
void printList(list<int> &l)
{
for (list<int>::iterator lit = l.begin(); lit != l.end(); lit++)
{
cout << *lit << " ";
}
cout << endl;
}
bool myCompare(int v1, int v2)
{
return v1 > v2;
}
void test01()
{
list<int> l1;
l1.push_back(10);
l1.push_back(20);
l1.push_back(30);
l1.push_back(40);
l1.push_front(1);
l1.push_front(2);
l1.push_front(3);
l1.push_front(4);
printList(l1);
l1.reverse();
printList(l1);
l1.sort();
printList(l1);
l1.sort(myCompare);//降序排列
printList(l1);
}
int main()
{
test01();
system("pause");
return 0;
}
4 3 2 1 10 20 30 40
40 30 20 10 1 2 3 4
1 2 3 4 10 20 30 40
40 30 20 10 4 3 2 1
请按任意键继续. . .
案例:
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cmath>
#include<fstream>
#include<algorithm>//标准算法头文件
#include<deque>
#include<stack>
#include<list>
using namespace std;
bool myCompare(int v1, int v2)
{
return v1 > v2;
}
class person
{
public:
string m_name;
int m_age;
int m_height;
person(string name, int age, int height)
{
this->m_name = name;
this->m_age = age;
this->m_height = height;
}
};
void printList(list<person> &l)
{
for (list<person>::iterator lit = l.begin(); lit != l.end(); lit++)
{
cout << "姓名:" << (*lit).m_name << " 年龄:" << (*lit).m_age << " 身高:" << (*lit).m_height << endl;
}
}
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("刘备", 35, 160);
person p2("关羽", 40, 200);
person p3("张飞", 55, 167);
person p4("貂蝉", 35, 155);
person p5("赵云", 45, 190);
person p6("曹操", 33, 181);
L.push_back(p1);
L.push_back(p2);
L.push_back(p3);
L.push_back(p4);
L.push_back(p5);
L.push_back(p6);
printList(L);
cout << "-----------------" << endl;
L.sort(compareperson);
printList(L);
}
int main()
{
test01();
system("pause");
return 0;
}
姓名:刘备 年龄:35 身高:160
姓名:关羽 年龄:40 身高:200
姓名:张飞 年龄:55 身高:167
姓名:貂蝉 年龄:35 身高:155
姓名:赵云 年龄:45 身高:190
姓名:曹操 年龄:33 身高:181
-----------------
姓名:曹操 年龄:33 身高:181
姓名:刘备 年龄:35 身高:160
姓名:貂蝉 年龄:35 身高:155
姓名:关羽 年龄:40 身高:200
姓名:赵云 年龄:45 身高:190
姓名:张飞 年龄:55 身高:167
请按任意键继续. . .
总结:
- 对于自定义数据类型,必须要指定排序规则,否则编译器不知道如何进行排序
- 高级排序只是在排序规则上再进行一次逻辑规则制定,并不复杂