一丶List容器介绍
List是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针实现的。
链表的组成: 链表是由一系列结点组成的。
链表的组成: 一个是存放数据的数据域,另一个是存放下一个结点地点的指针域。
STL中的链表是双向链表
二丶List容器的构造函数
函数原型
list<type> listName; //默认的构造函数
list(begin,end); //List容器在这段区间内拷贝给新函数
list(n,elem); //用n个elem填充list容器
list(const list& li); //拷贝构造函数
案例:
#include<iostream>
using namespace std;
#include<list>
void printList(const list<int>& l){
for (list<int>::const_iterator li = l.begin(); li != l.end(); li++)
{
cout << *li << " " ;
}
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);
cout << "L1为: ";
printList(L1);
list<int>L2(L1.begin(), (--L1.end()));
cout << "L2为: ";
printList(L2);
list<int>L3(5, 100);
cout << "L3为: ";
printList(L3);
list<int>L4(L2);
cout << "L4为: ";
printList(L4);
}
void main(){
test01();
system("pause");
return;
}
运行结果为:
三丶List赋值和交换
函数原型
asign(begin,end); //List容器在这段区间内拷贝给本身
asign(n,elem); //用n个elem填充list容器
list& operator=(const list& li); //重载操作符=
swap(li); //自身与li交换
案例:
#include<iostream>
using namespace std;
#include<list>
void printList(const list<int>& l){
for (list<int>::const_iterator li = l.begin(); li != l.end(); li++)
{
cout << *li << " " ;
}
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);
cout << "L1为: ";
printList(L1);
list<int>L2;
L2.assign(L1.begin(), (--L1.end()));
cout << "L2为: ";
printList(L2);
list<int>L3;
L3.assign(5, 100);
cout << "L3为: ";
printList(L3);
//一下两种方式都可以
// list<int>L4 = L2;
list<int>L4;
L4 = L2;
cout << "L4为: ";
printList(L4);
cout << "***********************************" << endl;
cout << "交换L3和L4后" << endl;
L3.swap(L4);
cout << "L3为: ";
printList(L3);
cout << "L4为: ";
printList(L4);
}
void main(){
test01();
system("pause");
return;
}
运行结果为:
四丶List大小操作
函数原型
size(); //返回容器的大小
empty(); //返回容器是否为空
resize(n); //重新分配容器大小,如果重新分配大的话用默认值填补,如果小的话截取
resize(n,elem); //重新分配容器大小,如果重新分配大的话用elem值填补,如果小的话截取
案例:
#include<iostream>
using namespace std;
#include<list>
void printList(const list<int>& l){
for (list<int>::const_iterator li = l.begin(); li != l.end(); li++)
{
cout << *li << " " ;
}
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);
cout << "L1为: ";
printList(L1);
list<int>L2;
L2.assign(L1.begin(), (--L1.end()));
cout << "L2为: ";
printList(L2);
list<int>L3;
L3.assign(5, 100);
cout << "L3为: ";
printList(L3);
cout << "L3的大小为:" << L3.size() << endl;
if (L3.empty()){
cout << "L3为空" << endl;
}
else
{
cout << "L3不为空" << endl;
}
//一下两种方式都可以
// list<int>L4 = L2;
list<int>L4;
L4 = L2;
cout << "L4为: ";
printList(L4);
cout << "L4的大小为:" << L4.size() << endl;
L4.resize(10);
cout << "L4重新分配大小之后为:" << endl;
printList(L4);
L4.resize(13,2);
cout << "L4第二次重新分配大小之后为:" << endl;
printList(L4);
}
void main(){
test01();
system("pause");
return;
}
运行结果为:
五丶List插入和删除
函数原型
push_back(elem); //从容器的尾部增加一个元素elem
pop_back(); //从容器的尾部移除第一个元素
push_front(elem); //从容器的头部增加一个元素elem
pop_front(); //从容器的头部移除第一个元素
insert(pos,elem); //在pos位置插入elem元素
insert(pos,n,elem); //在pos位置插入n个elem个元素
insert(pos,beg,end); //在pos位置插入[beg,end)区间的元素
clear(); //清空List容器的元素
erase(beg,end); //删除从beg到end位置的元素
erase(pos); //删除pos位置的元素,返回下一个元素的位置
remove(elem); //删除容器中所有跟elem值匹配的成员
案例:
#include<iostream>
using namespace std;
#include<list>
void printList(const list<int>& l){
for (list<int>::const_iterator li = l.begin(); li != l.end(); li++)
{
cout << *li << " ";
}
cout << endl;
}
void test01(){
list<int> L1;
//push_back(elem); //从容器的尾部增加一个元素elem
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
L1.push_back(50);
L1.push_back(60);
L1.push_back(70);
L1.push_back(80);
cout << "L1为: ";
printList(L1);
//push_front(elem); //从容器的头部增加一个元素elem
L1.push_front(10);
cout << "L1为: ";
printList(L1);
//pop_back(); //从容器的尾部移除第一个元素
L1.pop_back();
cout << "L1为: ";
printList(L1);
//pop_front(); //从容器的头部移除第一个元素
L1.pop_front();
cout << "L1为: ";
printList(L1);
//insert(pos,elem); //在pos位置插入elem元素
L1.insert(L1.begin(), 5);
cout << "L1为: ";
printList(L1);
//insert(pos,n,elem); //在pos位置插入n个elem个元素
L1.insert((++L1.begin()), 5,3);
cout << "L1为: ";
printList(L1);
list<int>L3;
L3.assign(5, 100);
//insert(pos,beg,end); //在pos位置插入[beg,end)区间的元素
L1.insert(L1.begin(), L3.begin(), L3.end());
cout << "L1为: ";
printList(L1);
//erase(beg,end); //删除从beg到end位置的元素
L1.erase(L1.begin(), ++(++L1.begin()));
cout << "L1为: ";
printList(L1);
//erase(pos); //删除pos位置的元素,返回下一个元素的位置
L1.erase(L1.begin());
cout << "L1为: ";
printList(L1);
//remove(elem); //删除容器中所有跟elem值匹配的成员
L1.remove(3);
cout << "L1为: ";
printList(L1);
//clear(); //清空List容器的元素
L1.clear();
cout << "L1为: ";
printList(L1);
}
void main(){
test01();
system("pause");
return;
}
运行结果为:
六丶List数据存取
函数原型
front(); //返回第一个元素
back(); //返回最后一个元素
实例为:
#include<iostream>
using namespace std;
#include<list>
void printList(const list<int>& l){
for (list<int>::const_iterator li = l.begin(); li != l.end(); li++)
{
cout << *li << " ";
}
cout << endl;
}
void test01(){
list<int> L1;
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
L1.push_back(50);
L1.push_back(60);
L1.push_back(70);
L1.push_back(80);
cout << "L1为: ";
printList(L1);
cout << "L1的第一个元素为:\t" << L1.front() << endl;
cout << "L1的最后一个元素为:\t" << L1.back() << endl;
}
void main(){
test01();
system("pause");
return;
}
运行结果为:
七丶List反转和排序
函数原型
reverse(); //反转链表
sort(); //对链表排序
实例为:
#include<iostream>
using namespace std;
#include<list>
void printList(const list<int>& l){
for (list<int>::const_iterator li = l.begin(); li != l.end(); li++)
{
cout << *li << " ";
}
cout << endl;
}
void test01(){
list<int> L1;
L1.push_back(90);
L1.push_back(80);
L1.push_back(40);
L1.push_back(50);
L1.push_back(60);
L1.push_back(70);
L1.push_back(20);
cout << "L1为: \t\t";
printList(L1);
L1.reverse();
cout << "L1反转之后为:\t";
printList(L1);
L1.sort();
cout << "L1排序之后为:\t";
printList(L1);
}
void main(){
test01();
system("pause");
return;
}
运行结果为:
博主学习的视频链接:(感觉讲得很好)
https://www.bilibili.com/video/av41559729?p=217