1、链表
优点:对任意位置快速插入删除
缺点:容器遍历速度没有数组快,占用空间比数组大(数据域4+指针域4=8)
2、list:双向循环链表
data(数据域)
prev(指向前一个结点)
next(指向下一个结点)
(最后一个结点的next指向第一个结点,第一个结点的prev指向最后一个结点)
push_front()
pop_front()
front() back()
begin() end()
push_back() pop_back()
L1.swap(L2)
L1.size()
L1.resize()
L1.empty()
insert(pos,elem)
insert(pos, n, elem)
insert(pos, begin, end)
erase(begin,end)
erase(pos)
remove(elem) //删除容器中所有与elem值匹配的元素
链表的迭代器:双向迭代器(前移,后移)
3、list的优点
采用动态存储分配,不会造成内存浪费溢出
链表插入删除很方便,修改指针就行,不用移动很远相关元素
4、list的缺点
链表灵活,,但是 空间(指针域) 和 时间(遍历)消耗大
5、总结
list插入删除操作不会造成原有list迭代器失效,vector中不成立
STL中,list和vector是最长使用的两个容器
1、list构造函数
#include<iostream>
using namespace std;
#include<list>
//list容器构造函数
void printList(const list<int>& L)
{
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
//1.默认构造
list<int>L1;
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
printList(L1);
//2.区间构造
list<int>L2(L1.begin(), L1.end());
printList(L2);
//3.n个elem
list<int>L3(4, 88);
printList(L3);
//4.拷贝构造
list<int>L4(L3);
printList(L4);
}
int main()
{
test01();
system("pause");
return 0;
}
2、list 赋值交换
#include<iostream>
using namespace std;
#include<list>
//list容器-赋值交换
void printList(const list<int>& L)
{
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//赋值
void test01()
{
//1.
list<int>L1;
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
printList(L1);
//2.
list<int>L2;
L2 = L1;
printList(L2);
//3.
list<int>L3;
L3.assign(L1.begin(), L1.end());
printList(L3);
//4.
list<int>L4;
L4.assign(5, 888);
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(7, 99);
cout << "交换前" << endl;
printList(L1);
printList(L2);
L1.swap(L2);
cout << "交换后" << endl;
printList(L1);
printList(L2);
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
3、list 大小操作
#include<iostream>
using namespace std;
#include<list>
//list大小操作
void printList(const list<int>& L)
{
for (list<int>::const_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);
L1.push_back(50);
printList(L1);
if (L1.empty())
{
cout << "L1为空" << endl;
}
else
{
cout << "L1不为空" << endl;
cout << "L1的大小:" << L1.size() << endl;
}
//L1.resize(10); //10个数,用默认0 来填充
L1.resize(10, 9); //10个数,用指定9 来填充
printList(L1);
L1.resize(4);
printList(L1);
}
int main()
{
test01();
system("pause");
return 0;
}
4、list 插入删除
insert(pos,elem)
insert(pos, n, elem)
insert(pos, begin, end)
erase(begin,end)
erase(pos)
remove(elem) //删除容器中所有与elem值匹配的元素
#include<iostream>
using namespace std;
#include<list>
//list插入删除
void printList(const list<int>& L)
{
for (list<int>::const_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_front(100);
L1.push_front(200);
L1.push_front(300);
// 300 200 100 10 20 30
printList(L1);
L1.pop_back();
// 300 200 100 10 20
printList(L1);
L1.pop_front();
// 200 100 10 20
printList(L1);
L1.insert(L1.begin(), 1000);
// 1000 200 100 10 20
printList(L1);
L1.erase(L1.begin());
// 200 100 10 20
printList(L1);
L1.push_back(20);
L1.push_back(60);
L1.push_back(20);
L1.push_back(90);
// 200 100 10 20 20 60 20 90
printList(L1);
L1.remove(20);
// 200 100 10 60 90
printList(L1);
//L1.erase(L1.begin(), L1.end());
L1.clear();
printList(L1);
}
int main()
{
test01();
system("pause");
return 0;
}
5、list 数据存取
front()
back()
6、
7、