c++的学习之路:14、list(1)

简介: c++的学习之路:14、list(1)

一、list介绍

首先还是看一看官方文档的介绍如下图,如下方五点:

1. list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。

2. list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向 其前一个元素和后一个元素。

3. list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,已让其更简单高 效。

4. 与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率 更好。

5. 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list 的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间 开销;list还需要一些额外的空间,以保存每个节点的相关联信息(对于存储类型较小元素的大list来说这 可能是一个重要的因素)

在图二就是一些参数,可以明显看到没有【】,因为list就是相当于一个带头双向链表,参数和之前学习的差不多,就不详细讲了。

 

二、增

如下方代码就是就创建了一个链表为l1和l2进行尾插和头插然后在利用for进行打印。


void Test1()
{
    list l1;
    l1.push_back(1);
    l1.push_back(2);
    l1.push_back(3);
    l1.push_back(4);
    list l2;
    l2.push_front(1);
    l2.push_front(2);
    l2.push_front(3);
    l2.push_front(4);
    for (auto li : l1)
    {
        cout << li << ' ';
    }
    cout << endl;
    for (auto li : l2)
    {
        cout << li << ' ';
    }
    cout << endl;
}

三、删

如下就是先尾删打印在进行头删打印,代码和测试如下。

void Test2()
{
    list l1;
    l1.push_back(1);
    l1.push_back(2);
    l1.push_back(3);
    l1.push_back(4);
    l1.push_back(5);
    l1.push_back(6);
    for (auto li : l1)
    {
        cout << li << ' ';
    }
    cout << endl;
    l1.pop_back();
    l1.pop_back();
    for (auto li : l1)
    {
        cout << li << ' ';
    }
    cout << endl;
    l1.pop_front();
    l1.pop_front();
    for (auto li : l1)
    {
        cout << li << ' ';
    }
    cout << endl;
}

 

四、查和改

下方代码就是利用find函数进行插在,找到了3然后返回位置给pos在这个地方进行插入30,然后在查找30再把这个删掉。

void Test3()
{
    list l1;
    l1.push_back(1);
    l1.push_back(2);
    l1.push_back(3);
    l1.push_back(4);
    l1.push_back(5);
    l1.push_back(6);
    for (auto li : l1)
    {
        cout << li << ' ';
    }
    cout << endl;
    auto pos = find(l1.begin(),l1.end(),3);
    l1.insert(pos, 30);
    for (auto li : l1)
    {
        cout << li << ' ';
    }
    cout << endl;
    pos= find(l1.begin(), l1.end(), 30);
    l1.erase(pos);
    for (auto li : l1)
    {
        cout << li << ' ';
    }
    cout << endl;
}

五、交换

交换两个链表的数值,这个原理就是交换头指针,如下方代码所示。

void Test4()
{
    list l1;
    l1.push_back(1);
    l1.push_back(2);
    l1.push_back(3);
    l1.push_back(4);
    l1.push_back(5);
    l1.push_back(6);
    list l2;
    l2.push_back(10);
    l2.push_back(20);
    l2.push_back(30);
    l2.push_back(40);
    l2.push_back(50);
    l2.push_back(60);
    for (auto li : l1)
    {
        cout << li << ' ';
    }
    cout << endl;
    for (auto li : l2)
    {
        cout << li << ' ';
    }
    cout << endl;
    l1.swap(l2);
    for (auto li : l1)
    {
        cout << li << ' ';
    }
    cout << endl;
    for (auto li : l2)
    {
        cout << li << ' ';
    }
    cout << endl;
}

六、代码

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <list>
using namespace std;
 
void Test1()
{
  list<int> l1;
  l1.push_back(1);
  l1.push_back(2);
  l1.push_back(3);
  l1.push_back(4);
  list<int> l2;
  l2.push_front(1);
  l2.push_front(2);
  l2.push_front(3);
  l2.push_front(4);
  for (auto li : l1)
  {
    cout << li << ' ';
  }
  cout << endl;
  for (auto li : l2)
  {
    cout << li << ' ';
  }
  cout << endl;
}
 
void Test2()
{
  list<int> l1;
  l1.push_back(1);
  l1.push_back(2);
  l1.push_back(3);
  l1.push_back(4);
  l1.push_back(5);
  l1.push_back(6);
  for (auto li : l1)
  {
    cout << li << ' ';
  }
  cout << endl;
  l1.pop_back();
  l1.pop_back();
  for (auto li : l1)
  {
    cout << li << ' ';
  }
  cout << endl;
  l1.pop_front();
  l1.pop_front();
  for (auto li : l1)
  {
    cout << li << ' ';
  }
  cout << endl;
}
 
void Test3()
{
  list<int> l1;
  l1.push_back(1);
  l1.push_back(2);
  l1.push_back(3);
  l1.push_back(4);
  l1.push_back(5);
  l1.push_back(6);
  for (auto li : l1)
  {
    cout << li << ' ';
  }
  cout << endl;
  auto pos = find(l1.begin(),l1.end(),3);
  l1.insert(pos, 30);
  for (auto li : l1)
  {
    cout << li << ' ';
  }
  cout << endl;
  pos= find(l1.begin(), l1.end(), 30);
  l1.erase(pos);
  for (auto li : l1)
  {
    cout << li << ' ';
  }
  cout << endl;
}
 
void Test4()
{
  list<int> l1;
  l1.push_back(1);
  l1.push_back(2);
  l1.push_back(3);
  l1.push_back(4);
  l1.push_back(5);
  l1.push_back(6);
  list<int> l2;
  l2.push_back(10);
  l2.push_back(20);
  l2.push_back(30);
  l2.push_back(40);
  l2.push_back(50);
  l2.push_back(60);
  for (auto li : l1)
  {
    cout << li << ' ';
  }
  cout << endl;
  for (auto li : l2)
  {
    cout << li << ' ';
  }
  cout << endl;
  l1.swap(l2);
  for (auto li : l1)
  {
    cout << li << ' ';
  }
  cout << endl;
  for (auto li : l2)
  {
    cout << li << ' ';
  }
  cout << endl;
}
 
int main()
{
  Test4();
}
目录
相关文章
|
2天前
|
编译器 开发工具 C语言
配置C++的学习环境
这篇教程介绍了学习C++语言所需的环境配置和软件选择。首先,你需要一个文本编辑器(如Visual Studio Code、Visual Studio、Vim、Emacs或Eclipse)和一个C++编译器(如GCC)。在不同操作系统上安装GCC的方法包括:在Linux或UNIX上使用命令行检查或安装GCC,在Mac OS X上通过Apple的Xcode,而在Windows上则需要安装MinGW。教程还提供了使用Visual Studio创建和编译C++程序的步骤。最后,文章简述了g++编译器的使用及其常用命令选项。
9 0
|
1天前
|
编译器 C语言 C++
从C语言到C++_33(C++11_上)initializer_list+右值引用+完美转发+移动构造/赋值(下)
从C语言到C++_33(C++11_上)initializer_list+右值引用+完美转发+移动构造/赋值
7 1
|
1天前
|
编译器 C语言 C++
从C语言到C++_33(C++11_上)initializer_list+右值引用+完美转发+移动构造/赋值(中)
从C语言到C++_33(C++11_上)initializer_list+右值引用+完美转发+移动构造/赋值
7 1
从C语言到C++_33(C++11_上)initializer_list+右值引用+完美转发+移动构造/赋值(中)
|
1天前
|
存储 安全 C语言
从C语言到C++_33(C++11_上)initializer_list+右值引用+完美转发+移动构造/赋值(上)
从C语言到C++_33(C++11_上)initializer_list+右值引用+完美转发+移动构造/赋值
9 2
|
2天前
|
C语言 容器
从C语言到C++_17(list的模拟实现)list不是原生指针的迭代器(下 )
从C语言到C++_17(list的模拟实现)list不是原生指针的迭代器
5 1
|
2天前
|
C语言 计算机视觉
从C语言到C++_17(list的模拟实现)list不是原生指针的迭代器(中)
从C语言到C++_17(list的模拟实现)list不是原生指针的迭代器
10 1
|
2天前
|
存储 算法 编译器
从C语言到C++_17(list的模拟实现)list不是原生指针的迭代器(上)
从C语言到C++_17(list的模拟实现)list不是原生指针的迭代器
8 1
|
2天前
|
存储 算法 C语言
从C语言到C++_16(list的介绍和常用接口函数)
从C语言到C++_16(list的介绍和常用接口函数)
2 0
|
6天前
|
存储 C++ 容器
黑马c++ STL部分 笔记(7) list容器
黑马c++ STL部分 笔记(7) list容器
|
3天前
|
存储 编译器 程序员
c++存储类
c++存储类
19 3
http://www.vxiaotou.com