欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > 【C++】List

【C++】List

2024/10/26 15:17:28 来源:https://blog.csdn.net/m0_46676283/article/details/142752079  浏览:    关键词:【C++】List

文章目录

  • 1.list的介绍及使用
    • 1.1 list的介绍
    • 1.2 list的使用
      • 1.2.1 list的构造
      • 1.2.2 list iterator的使用
      • 1.2.3 list capacity
      • 1.2.4 list element access
      • 1.2.5 list modifiers

本节目标
1.list的介绍及使用
2.list的深度剖析及模拟实现
3.list与vector的对比
4.本节习题

1.list的介绍及使用

1.1 list的介绍

list的文档介绍
1.list是可以在常数范围内任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。
2.list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。
3.list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,已让其更简单高效。
4.与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率更好。
5.与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间开销;list还需要一些额外的空间,以保存每个节点的相关联信息(对于存储类型较小元素的大list来说这可能是一个重要的因素)。

1.2 list的使用

list中的接口比较多,此处类似,只需要掌握如何正确的使用,然后再去深入研究背后的原理,已达到可扩展的能力。以下为list中一些常见的重要接口。

1.2.1 list的构造

构造函数(constructor)接口说明
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
// constructing lists
#include <iostream>
#include <list>int main()
{std::list<int> l1;	// 构造空的l1std::list<int> l2 (4, 100);  // l2中放4个值为100的元素std::list<int> l3 (l2.begin(), l2.end()); //用l2的[begin(), end())左闭右开的区间构造l3std::list<int> l4(l3);    // 用l3拷贝构造l4// 以数组为迭代器区间构造l5int arrat[] = {16, 2, 77, 99};std::list<int> l5(array, array + sizeof(array)/sizeof(int));// 用迭代器方式打印l5中的元素for(std::list<int>::iterator it = l5.begin(); it != l5.end(); it++)std::cout << *it << " ";std::cout<<endl;return 0;
}

1.2.2 list iterator的使用

此处,大家可暂时将迭代器理解成一个指针,该指针指向list中的某个节点。

函数声明接口说明
begin+end返回第一个元素的迭代器+返回最后一个元素下一个位置的迭代器
rbegin + rend返回第一个元素的reverse_iterator,即end位置,+返回最后一个元素下一个位置的reverse_iterator,即begin位置

在这里插入图片描述
注意:
1.begin与end为正向迭代器,对迭代器执行++操作,迭代器向后移动
2.rbegin(end)与rend(begin)为反向迭代器,对迭代器执行++操作,迭代器向前移动

#include <iostream>
using namespace std;
#include <list>void print_list(const list<int>& l)
{// 注意这里调用的是list的begin() const, 返回list的const_iterator对象for (list<int>::const_iterator it = l.begin(); it != l.end(); it++){cout << *it << " ";// *it = 10;编译不通过}cout << endl;
}
int main()
{int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};list<int> l(array, array + sizeof(array) / sizeof(array[0]));//使用正向迭代器正向打印list中的元素for (list<int>::iterator it = l.begin(); it != l.end(); ++it)cout << *it << " ";cout << endl;// 使用反向迭代器逆向打印list中的元素for (list<int>::reverse_iterator it = l.rbegin();++it)cout << *it << " ";cout << endl;return 0;

1.2.3 list capacity

函数声明接口说明
empty检测list是否为空,是返回true,否则返回false
size返回list中有效节点的个数

1.2.4 list element access

函数声明接口说明
front返回list中第一个节点的值的引用
back返回list的最后一个节点的值的引用

1.2.5 list modifiers

函数声明接口说明
push_front在list首元素前插入值为val的元素(头插)
pop_front删除list中第一个元素(头删)
push_back在list尾部插入值为val的元素(尾插)
push_back删除list中最后一个元素(尾删)
insert在list position位置中插入值为val的元素
erase删除list position位置的元素
swap交换两个list中的元素
clear清空list中的有效元素
#include <iostream>
#include <list>
using namespace std;void PrintList(list<int>& l)
{for(auto& e : l)cout << e << " ";cout << endl;
}// ================================
// 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,头部插入0L.push_back(4);L.push_front(0);PrintList(L);//删除list尾部节点和头部节点L.pop_back();L.pop_front();PrintList(L);
}未写完

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com