list类是一种可在任意位置插入的容器
list类的头文件
#include <list>
一.list类的构造
1.无参构造
2.初始化n个值的构造
3.拷贝构造
4.用区间构造
5.迭代器构造
int main()
{
list<int>t1;
list<int>t2(10,2);
list<int>t3(t2);
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int len = sizeof(arr) / sizeof(arr[0]);
list<int>t4(arr, arr + len);
list<int>t5=t3;
auto p = find(t4.begin(),t4.end(),4);
list<int>t5(p, t4.end());
return 0;
}
注:
list (size_type n, const value_type& val =value_type())在构造时,不传val默认值为0,传了参数以值为参数
二.list类的大小操作
1.size
2.empty
3.clear
4.resize
5.front
6.back
int main()
{
list<int>t1(10, 2);
cout << t1.size() << endl;
t1.resize(50);
cout << t1.size() << endl;
cout << t1.empty() << endl;
t1.clear();
cout << t1.size() << endl;
cout<<t1.front()<<endl;
cout<<t1.back()<<endl;
return 0;
}
注:
1.list类是没有容量的概念的,所以没有capacity和reserve函数
2.clear函数会将list所有的节点清空,所以clear后,size为0
3.resize (size_type n, value_type val = value_type()),n<=size(),取list的前n个,当n>size(),不传val默认以0填充,传参数以参数填充
三.list类的访问
1.迭代器访问
2.C++11for循环访问
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int len = sizeof(arr) / sizeof(arr[0]);
list<int>t1(arr,arr+len);
list<int>::iterator it = t1.begin();//迭代器
while (it!= t1.end())
{
cout << *it << "-->";
it++;
}
cout << "over" << endl;
list<int>::reverse_iterator rit = t1.rbegin();//反向迭代器
while (rit != t1.rend())
{
cout << *rit << "-->";
rit++;
}
cout << "over" << endl;
for (auto e : t1)
{
cout << e << "-->";
}
cout << "over" << endl;
return 0;
}
注:
list类是不支持 [ ] 访问的
四.list类的数据操作
1.push_back
2.push_front
3.pop_back
4.pop_front
5.insert
6.erase
7.swap
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int len = sizeof(arr) / sizeof(arr[0]);
list<int>t1(arr,arr+len);
t1.push_back(100);
t1.push_front(20);
t1.pop_back();
t1.pop_front();
for (auto e : t1)
{
cout << e << "-->";
}
cout << "over" << endl;
auto p = find(t1.begin(),t1.end(),4);
//插入的3种方式
//在指定位置插入一个值
t1.insert(p,100);
//在指定位置插入n个值
t1.insert(p, 5, 50);
//在指定位置插入一段区间的所有值
list<int>t2 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto p1 = find(t1.begin(), t1.end(), 4);
auto p2 = find(t1.begin(), t1.end(), 8);
t1.insert(p, p1, p2);
for (auto e : t1)
{
cout << e << "-->";
}
cout << "over" << endl;
//删除的两种方式
list<int>t3 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto p4 = find(t3.begin(), t3.end(), 3);
auto p5 = find(t3.begin(), t3.end(), 6);
auto p6 = find(t3.begin(), t3.end(), 9);
//删除某个位置的值
t3.erase(p6);
// 删除一段区间的所有值
t3.erase(p4, p5);
for (auto e : t3)
{
cout << e << "-->";
}
cout << "over" << endl;
list<int>t5 = { 1, 2, 3, 4, 5 };
list<int>t6 = { 6, 7, 8, 9, 10 };
for (auto e : t5)
{
cout << e << "-->";
}
cout << "over" << endl;
t5.swap(t6);
for (auto e : t5)
{
cout << e << "-->";
}
cout << "over" << endl;
return 0;
}
注:
1.删除一段区间的值,这个区间是前闭后开的,即[ start,finish ),删除不包括后面位置的值
2.当删除迭代器所指位置的值后,会造成迭代器失效,即迭代器所指的位置无法访问了,有两种解决办法 第一种:删除之后不使用该迭代器 第二种:用该迭代器接收erase之后的返回值,该迭代器就会指向原本迭代器所指位置的下一个位置
int main()
{
list<int>t = { 1, 2, 3, 4, 5 };
auto p = t.begin();
cout << *p << endl;//此时*p = 1
t.erase(p);
cout << *p << endl;//程序会崩溃,因为p所指的节点已经被释放掉了
return 0;
}
int main()
{
list<int>t = { 1, 2, 3, 4, 5 };
auto p = t.begin();
cout << *p << endl;//此时*p = 1
p = t.erase(p);
cout << *p << endl;//*p = 2
return 0;
}