STL_LIST
STL_LIST
1.list的介绍
- list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向
迭代。
- list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通
过指针指向其前一个元素和后一个元素。
- list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,已让
其更简单高效。
- 与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素
的执行效率更好。
- 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问。
2.list的使用
构造函数) 接口说明
list() 构造空的list
list (size_type n, const value_type& val = value_type()) 构造的list中包含n个值为val的 元素
list (const list& x) 拷贝构造函数
list (InputIterator fifirst, InputIterator last) 用[first, last)区间中的元素构造list
代码实现
#include<iostream>
#include<list>
using namespace std;
void main()
{
std::list<int> l1; //构造空的l1
std::list<int> l2(4, 100); //l2中放四个值为100的元素.
std::list<int> l3(l2); //拷贝构造函数
std::list<int> l4(l2.begin(), l2.end()); //l2的左闭右开区间构造l4
//以数组为迭代器区间构造l5
int a[] = { 16, 2, 77, 29 };
std::list<int> l5(a, a + sizeof(a) / sizeof(int));
//以迭代器方式打印l5中的元素
std::list<int>::iterator it = l5.begin();
while (it != l5.end())
{
if (it != l5.end())
{
std::cout << *it << " ";
++it;
}
}
std::cout <<std::endl;
//C++11方式遍历
for (auto &e : l5)
std::cout << e << " ";
std::cout << std::endl;
}
3.list iterator的使用
函数声明 接口说明
begin + end 返回第一个元素的迭代器+返回最后一个元素下一个位置的迭代器
rbegin + rend 返回第一个元素的reverse_iterator,即end位置,返回最后一个元素下一个位 reverse_iterator,即begin的位置
【注意】
- begin与end为正向迭代器,对迭代器执行++操作,迭代器向后移动
- rbegin(end)与rend(begin)为反向迭代器,对迭代器执行++操作,迭代器向前移动
void main()
{
int a[] = { 10, 20, 30, 40 };
list<int> l(a,a+sizeof(a)/sizeof(int));
for (std::list<int>::iterator it = l.begin(); it != l.end(); ++it)
cout << *it << " ";
cout << endl;
list<int> l1(l);
for (std::list<int>::reverse_iterator rit = l1.rbegin(); rit != l1.rend(); ++rit)
cout << *rit << " ";
cout << endl;
}
4.list中各种函数用法
函数声明 接口说明
empty 检测list是否为空,是返回true,否则返回false
size 返回list中有效节点的个数
front 返回list的第一个节点中值的引用
back 返回list的最后一个节点中值的引用
push_front 在list首元素前插入值为val的元素
pop_front 删除list中第一个元素
push_back 在list尾部插入值为val的元素
pop_back 删除list中最后一个元素
insert 在list position 位置中插入值为val的元素
erase 删除list position位置的元素
swap 交换两个list中的元素
clear 清空list中的有效元素
resize 调整容器大小
代码
//true if the container size is 0, false otherwise.
int main()
{
std::list<int> mylist;
int sum(0);
for (int i = 1; i <= 10; ++i)
mylist.push_back(i);
while (!mylist.empty())
{
sum += mylist.front();
mylist.pop_front();
}
std::cout << "total: " << sum << '\n';
return 0;
}
//Returns the number of elements in the list container.
// list::size
#include <iostream>
#include <list>
int main ()
{
std::list<int> myints;
std::cout << "0. size: " << myints.size() << '\n';
for (int i=0; i<10; i++) myints.push_back(i);
std::cout << "1. size: " << myints.size() << '\n';
myints.insert (myints.begin(),10,100);
std::cout << "2. size: " << myints.size() << '\n';
myints.pop_back();
std::cout << "3. size: " << myints.size() << '\n';
return 0;
}
// list::front
int main ()
{
std::list<int> mylist;
mylist.push_back(77);
mylist.push_back(22);
// now front equals 77, and back 22
mylist.front() -= mylist.back();
std::cout << "mylist.front() is now " << mylist.front() << '\n';
return 0;
}
// push_back/pop_back/push_front/pop_front
void TestList1()
{
int array[] = { 1, 2, 3 };
list<int> L(array, array+sizeof(array)/sizeof(array[0]));
// 在list的尾部插入4,头部插入0
L.push_back(4);
L.push_front(0);
PrintList(L);
// 删除list尾部节点和头部节点
L.pop_back();
L.pop_front();
PrintList(L);
}
//=========================================================================
================
// insert /erase
void TestList3()
{
int array1[] = { 1, 2, 3 };
list<int> L(array1, array1+sizeof(array1)/sizeof(array1[0]));
// 获取链表中第二个节点
auto pos = ++L.begin();
cout << *pos << endl;
// 在pos前插入值为4的元素
L.insert(pos, 4);
PrintList(L);
// 在pos前插入5个值为5的元素
L.insert(pos, 5, 5);
PrintList(L);
// 在pos前插入[v.begin(), v.end)区间中的元素
vector<int> v{ 7, 8, 9 };
L.insert(pos, v.begin(), v.end());
PrintList(L);
// 删除pos位置上的元素
L.erase(pos);
PrintList(L);
// 删除list中[begin, end)区间中的元素,即删除list中的所有元素
L.erase(L.begin(), L.end());
PrintList(L);
}
// swap/clear
void TestList4()
{
// 用数组来构造list
int array1[] = { 1, 2, 3 };
list<int> l1(array1, array1+sizeof(array1)/sizeof(array1[0]));
PrintList(l1);
// 交换l1和l2中的元素
l1.swap(l2);
PrintList(l1);
PrintList(12);
//清空l2中的元素
l2.clear();
cout<<l2.size()<<endl;
}
//resize 调整容器大小
int main ()
{
std::list<int> mylist;
// set some initial content:
for (int i=1; i<10; ++i) mylist.push_back(i);
mylist.resize(5);
mylist.resize(8,100);
mylist.resize(12);
std::cout << "mylist contains:";
for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
关于list还有许多操作,具体可以参考cplusplus网站进行学习.
5.list的迭代器失效
迭代器失效即迭代器所指向的节点的 效,即该节点被删除了。因为list的底层结构为带头结点的双向循环链表,因此在list中进行插入 时是不会导致list的迭代器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭 代器,其他迭代器不会受到影响。
代码
void TestListIterator1()
{
int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
list<int> l(array, array + sizeof(array) / sizeof(array[0]));
auto it = l.begin();
while (it != l.end())
{
// erase()函数执行后,it所指向的节点已被删除,因此it无效,在下一次使用it时,必须先给其赋值
l.erase(it);
++it;
}
}
// 改正
void TestListIterator()
{
int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
list<int> l(array, array + sizeof(array) / sizeof(array[0]));
auto it = l.begin();
while (it != l.end())
{
l.erase(it++); // it = l.erase(it);
}
}