C++——std::List

本文深入探讨C++中的std::list,包括构造、赋值、迭代器、容量、元素访问、修改和操作。std::list是双向链表实现,适用于频繁的插入和删除操作。文章介绍了list的构造函数、赋值操作、遍历方式、容量函数、元素访问方法以及各种修改和操作函数,如push_front、push_back、pop_front、pop_back等,强调了其在链表操作中的优势和适用场景。
摘要由CSDN通过智能技术生成

写在前面

这一篇博客系统学习一下C++中List类的相关函数。之所以要学习这个类,是由于LeetCode中做到了Linked List类,虽然在LeetCode中并没有使用到List类的相关函数,而是主要利用链表的数据结构来完成一些内容,或者说,就是从基础的数据结构来实现接下来要学习的一些链表类的函数,比如:插入,删除结点等等。这篇博客的风格内容与之前博客并无两异,所以主要目的还是对List类的主要函数有个印象,在后面做题的时候,万一有遇到类似功能的需求,可以尝试通过函数使用来完成,以便达到简化程序的目的,并且不断强化对于基础类的学习。

list类与头文件包含

列表是序列容器,允许在序列中的任何地方进行恒定的时间插入和擦除操作,并在两个方向上进行迭代。

列表容器被实现为双向链表。双链表可以将它们包含的每个元素存储在不同和不相关的存储位置中。这个顺序是通过关联保存在一个到它之前的元素的链接的每个元素和一个到它之后的元素的链接上。

它们与forward_list非常相似:主要的区别是forward_list对象是单链表,因此它们只能向前迭代,以换取更小更有效的交换。

与其他基本标准序列容器(数组,矢量和双变量)相比,列表通常在已经获得迭代器的容器内的任何位置上插入,提取和移动元素,并且因此在执行密集使用的算法中这些,如排序算法。

列表和forward_lists与这些其他序列容器相比的主要缺点是它们不能直接访问元素的位置;例如,要访问列表中的第六个元素,必须从已知位置(如开始或结束)迭代到该位置,这需要线性时间。它们还消耗一些额外的内存来保持与每个元素相关联的链接信息(这可能是大型小元素列表的重要因素)。

list类引用:

#include <list>

list::list

以下代码是list构造的几种方法,代码来自:std::list::list

// constructing lists
#include <iostream>
#include <list>

int main ()
{
  // constructors used in the same order as described above:
  std::list<int> first;                                // empty list of ints
  std::list<int> second (4,100);                       // four ints with value 100
  std::list<int> third (second.begin(),second.end());  // iterating through second
  std::list<int> fourth (third);                       // a copy of third

  // the iterator constructor can also be used to construct from arrays:
  int myints[] = {
  16,2,77,29};
  std::list<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );

  std::cout << "The contents of fifth are: ";
  for (std::list<int>::iterator it = fifth.begin(); it != fifth.end(); it++)
    std::cout << *it << ' ';

  std::cout << '\n';

  return 0;
}

list::operator=

将新内容分配给容器,替换其当前内容,并相应地修改其大小。

// assignment operator with lists
#include <iostream>
#include <list>

int main ()
{
  std::list<int> first (3);      // list of 3 zero-initialized ints
  std::list<int> second (5);     // list of 5 zero-initialized ints

  second = first;
  first = std::list<int>();

  std::cout << "Size of first: " << int (first.size()) << '\n';
  std::cout << "Size of second: " << int (second.size()) << '\n';
  return 0;
// Output:
//Size of first: 0
//Size of second: 3
}

int元素的两个列表容器都被初始化为不同大小的序列。 然后,第二个分配给第一个,所以两个现在是相等的,大小为3.然后,首先分配给一个新构建的空容器对象,所以它的大小最终为0。

list-Iterators

list也有迭代器,迭代器(iterator)有时又称游标(cursor)是程序设计的软件设计模式,可在容器(container,例如链表或阵列)上遍访的接口,设计人员无需关心容器的内容。

迭代器(iterator)是一种对象,它能够用来遍历标准模板库容器中的部分或全部元素,每个迭代器对象代表容器中的确定的地址。迭代器修改了常规指针的接口,所谓迭代器是一种概念上的抽象:那些行为上像迭代器的东西都可以叫做迭代器。然而迭代器有很多不同的能力,它可以把抽象容器和通用算法有机的统一起来。

迭代器提供一些基本操作符:*、++、==、!=、=。这些操作和C/C++“操作array元素”时的指针接口一致。不同之处在于,迭代器是个所谓的复杂的指针,具有遍历复杂数据结构的能力。其下层运行机制取决于其所遍历的数据结构。因此,每一种容器型都必须提供自己的迭代器。事实上每一种容器都将其迭代器以嵌套的方式定义于内部。因此各种迭代器的接口相同,型号却不同。这直接导出了泛型程序设计的概念:所有操作行为都使用相同接口,虽然它们的型别不同。

这里写图片描述

利用迭代器也可以实现list的遍历,代码如下:

// list::begin
#include <iostream>
#include <list>

int main ()
{
  int myints[] = {
  75,23,65,42,13};
  std::list<int> mylist (myints,myints+5);

  std::cout << "mylist contains:";
  for (std::list<int>::iterator it=mylist.begin(); it != mylist.end(); ++it)
    std::cout << ' ' << *it;

  std::cout << '\n';

  return 0;
}
//Output:
//mylist contains: 75 23 65 42 13

list-Capacity:

这里写图片描述

1. list::empty: 判断容器是否为空。该函数无参数,返回值为:如果容器为空,则返回true,如果容器不为空,则返回false。这个函数不会改变容器,如果要清空容器,可以使用函数:list::clear。示例代码通过判断容器不为空,来实现容器的遍历,当然这个方法也可以用于其他容器,比如:vector、set、map等,代码如下:

// list::empty
#include <iostream>
#include <list>

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;
}
//Output:
//total: 55

2. list::size: 返回容器所含元素的个数。该函数没有参数传入。示例代码展示了通过list::size()函数来获取容器容量不同的时候容量大小。代码如下:

// 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 
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值