目录
list的使用及介绍
list的介绍
1、列表是序列容器,允许在序列中的任何位置进行恒定时间的插入和擦除操作,并在两个方向上进行迭代。
2、列表容器以双向链接列表的形式实现;双链列表可以将它们包含的每个元素存储在不同且不相关的存储位置。通过与到它前面的元素的链接和到它后面的元素的链接的每个元素的关联,在内部保持排序。
3、它们与forward_list非常相似:主要区别在于forward_list对象是单链接列表,因此只能进行迭代迭代,以换取更小,更有效的交换。
4、与其他基本标准序列容器相比(数组(向量和双端队列),列表通常在将元素插入,提取和移动容器中已经获得迭代器的任何位置中,以及因此在大量使用这些元素的算法(例如排序算法)中插入,提取和移动元素时,表现更好。与这些其他序列容器相比,list s和forward_list的主要缺点是它们无法通过位置直接访问元素。例如,访问列表中的第六个元素,则必须从一个已知位置(例如开始或结束)迭代到该位置,这需要在它们之间的距离中花费线性时间。它们还会消耗一些额外的内存来保持与每个元素相关联的链接信息(这可能是大型元素列表的重要因素)。
list的使用
list中的接口比较多,此处类似,只需要掌握如何正确的使用,然后再去深入研究背后的原理,已达到可扩展的能力。以下为list中一些常见的重要接口。
list的构造
list() 构造空的list
list (size_type n, const value_type& val = value_type()) 构造的list中包含n个值为val的元素
list (const list& x) 拷贝构造函数
list (InputIterator first, InputIterator last) 用[first, last)区间中的元素构造list
#include<iostream>
#include<list>
using namespace std;
int main() {
list<int> s1;//构造空的list
for (auto& e : s1) {
cout << e << " ";
}
cout << endl;
list<int> s2(5, 5);//构造的list中包含5个值为5的元素
for (auto& e : s2) {
cout << e << " ";
}
cout << endl;
list<int> s3(s2);//拷贝构造函数
for (auto& e : s3) {
cout << e << " ";
}
cout << endl;
list<int> s4(s2.begin(),s2.end());//用[begin, end)区间中的元素构造list
for (auto& e : s4) {
cout << e << " ";
}
cout << endl;
// 以数组为迭代器区间构造s5
int array[] = { 16,2,77,29 };
std::list<int> s5(array, array + sizeof(array) / sizeof(int));
for (auto& e : s5) {
cout << e << " ";
}
cout << endl;
return 0;
}
list iterator的使用
此处,大家可暂时将迭代器理解成一个指针,该指针指向list中的某个节点。
begin +end 返回第一个元素的迭代器+返回最后一个元素下一个位置的迭代器 rbegin +rend 返回第一个元素的reverse_iterator,即end位置,返回最后一个元素下一个位置reverse_iterator,即begin位置
- begin与end为正向迭代器,对迭代器执行++操作,迭代器向后移动
- rbegin(end)与rend(begin)为反向迭代器,对迭代器执行++操作,迭代器向前移动
#include<iostream>
#include<list>
using namespace std;
void prin(const list<int>& s) {
list<int>::const_iterator it = s.begin();
while (it != s.end()) {
//*it = 10;//迭代器为cnost不可修改
cout << *it << " ";
it++;
}
cout << endl;
}
int main() {
// 以数组为迭代器区间构造s5
int array[] = { 16,2,77,29 };
std::list<int> s5(array, array + sizeof(array) / sizeof(int));
prin(s5);
//反向迭代器
list<int>::reverse_iterator it = s5.rbegin();
while (it != s5.rend()) {
cout << *it << " ";
it++;
}
cout << endl;
return 0;
}
list capacity
empty 检测list是否为空,是返回true,否则返回false
size 返回list中有效节点的个数
#include<iostream>
#include<list>
using namespace std;
int main() {
list<int> s;
cout << s.empty() << endl;//判断s是否为空
list<int> s1(4,5);
cout << s1.size() << endl;//输出s1的有效节点
list<int>::reverse_iterator it = s1.rbegin();
while (it != s1.rend()) {
cout << *it << " ";
it++;
}
cout << endl;
return 0;
}
list element access
front 返回list的第一个节点中值的引用
back 返回list的最后一个节点中值的引用
#include<iostream>
#include<list>
using namespace std;
int main() {
list<int> s1;
s1.push_back(1);
s1.push_back(2);
s1.push_back(3);
s1.push_back(4);
s1.push_back(5);
cout << s1.front() << endl;
cout << s1.back() << endl;
list<int>::reverse_iterator it = s1.rbegin();
while (it != s1.rend()) {
cout << *it << " ";
it++;
}
cout << endl;
return 0;
}
这里返回首节点和尾节点的值
list modifiers
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中的有效元素
#include<iostream>
#include<list>
using namespace std;
int main() {
list<int> s(2, 11);
list<int> s1;
s1.push_back(1);
s1.push_back(2);
s1.push_back(3);
s1.push_back(4);
s1.push_back(5);
s1.push_front(0);//首部插入0
s1.push_back(6);//尾部插入6.
auto pso = ++s1.begin();//获取第二个节点
s1.insert(pso, 10);//在pos前边插入10
list<int>::iterator it = s1.begin();
while (it != s1.end()) {
cout << *it << " ";
it++;
}
cout << endl;
s1.insert(pso, 4,0);//在pos前边插入4个0
list<int>::iterator it1 = s1.begin();
while (it1 != s1.end()) {
cout << *it1 << " ";
it1++;
}
cout << endl;
s1.insert(pso, s.begin(), s.end());//在pos前边插入 s.begin(), s.end()
list<int>::iterator it2= s1.begin();
while (it2!= s1.end()) {
cout << *it2 << " ";
it2++;
}
cout << endl;
return 0;
}
#include<iostream>
#include<list>
using namespace std;
int main() {
list<int> s(2, 11);
list<int> s1;
s1.push_back(1);
s1.push_back(2);
s1.push_back(3);
s1.push_back(4);
s1.push_back(5);
s1.push_front(0);//首部插入0
s1.push_back(6);//尾部插入6.
auto pso = ++s1.begin();//获取第二个节点
s1.erase(pso++);//删除pso上的值
list<int>::iterator it1 = s1.begin();
while (it1 != s1.end()) {
cout << *it1 << " ";
it1++;
}
cout << endl;
s1.erase(pso, s1.end());//删除pso, s1.end()的值
list<int>::iterator it2 = s1.begin();
while (it2 != s1.end()) {
cout << *it2 << " ";
it2++;
}
cout << endl;
return 0;
}
#include<iostream>
#include<list>
using namespace std;
int main() {
list<int> s;
list<int> s1;
s1.push_back(1);
s1.push_back(2);
s1.push_back(3);
s1.push_back(4);
s1.push_back(5);
s1.push_front(0);//首部插入0
s1.push_back(6);//尾部插入6.
s.swap(s1);
list<int>::iterator it2 = s.begin();
while (it2 != s.end()) {
cout << *it2 << " ";
it2++;
}
cout << endl;
list<int>::iterator it3 = s1.begin();
while (it3 != s1.end()) {
cout << *it3 << " ";
it3++;
}
cout << endl;
return 0;
}
交换后把s1置为空
迭代器失效
#include<iostream>
#include<list>
using namespace std;
int main() {
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;
}
return 0;
}
erase()函数执行后,it所指向的节点已被删除,因此it无效,在下一次使用it时,必须先给其赋值
修改后
#include<iostream>
#include<list>
using namespace std;
int main() {
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())
{
if (*it % 2 == 0) {
it = l.erase(it);//l.erase(it++);
}
else {
++it;
}
}
auto it1 = l.begin();
while (it1 != l.end())
{
cout << *it1<< " ";
it1++;
}
cout << endl;
return 0;
}