C++_list快速学习入门(中英文结合)

简介: C++_list快速学习入门(中英文结合)

list简介

简介:链表,是一种在物理存储单元上非连续的存储结构。

list构造函数

函数原型:

  • list<T> lst; // list采用模板类实现,对象的默认构造形式
  • list(beg,end);// 构造函数[beg,end)区间中的元素拷贝给本身
  • list(n,elem);// 构造函数将n个elem拷贝被本身
  • list(const list &list);// 拷贝构造函数

测试代码:

#inclue<iostream>
using namespace std;
#include<list>
// 遍历容器的方法
void PrintList(const list<int>& L)
{
  for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
  {
    cout << *it << " ";
  }
  cout << endl;
}
void test01()
{
  // Create a list container
    // 创建容器
  list<int>L1;
  // Add data 
    // 添加数据
  L1.push_back(10);
  L1.push_back(20);
  L1.push_back(30);
  L1.push_back(40);
  // Traverse container
    // 遍历容器
  cout << "L1: ";
  PrintList(L1);
  cout << endl;
  // Constructed in the way of interval
    // 以区间形式拷贝
  list<int>L2(L1.begin(), L1.end());  
  cout << "L2: ";
  PrintList(L2);
  cout << endl;
  // Copy constructer 
    // 拷贝构造方式构建
  list<int>L3(L2);
  cout << "L3: ";
  PrintList(L3);
  cout << endl;
  // n elements
    // 构造函数将n个elem拷贝被本身
  list<int>L4(10, 1000);
  cout << "L4: ";
  PrintList(L4);
  cout << endl;
}
int main()
{
  test01();
  return 0;
}

运行结果:

总结:list构造方式同其他几个STL常用容器相似。

Summary: list is similar to several other STL containers.

list 赋值和交换

  • assign(beg,end); // 将[beg,end)区间中的数据拷贝赋值给本身
  • assign(n,elem); // 将n个elem拷贝赋值给本身
  • list& operator=(const list &lst);// 重载等号操作符
  • swap(lst); // 将lst与本身的元素互换
#include<iostream>
#include<list>
// Assignment and exchange of list container
using namespace std;
void PrintList(const list<int>& L)
{
  for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
  {
    cout << *it << " ";
  }
  cout << endl;
}
// Assignment 
void test01()
{
  list<int>L1;
  L1.push_back(10);
  L1.push_back(20);
  L1.push_back(30);
  L1.push_back(40);
  cout << "L1: ";
  PrintList(L1);
  cout << endl;
  list<int>L2;
  L2 = L1; // list& operator = (const list & list)
  cout << "L2: ";
  PrintList(L2);
  cout << endl;
  list<int>L3;
    // assign(beg,end);  // 将[beg,end)区间中的数据拷贝赋值给本身
  L3.assign(L2.begin(), L2.end());
  cout << "L3: ";
  PrintList(L3);
  cout << endl;
  list<int>L4;
    // assign(n,elem); // 将n个elem拷贝赋值给本身
  L4.assign(10, 100);
  cout << "L4: ";
  PrintList(L4);
  cout << endl;
}
// Swap
void test02()
{
  list<int>L1;
  L1.push_back(10);
  L1.push_back(20);
  L1.push_back(30);
  L1.push_back(40);
  list<int>L2;
  L2.assign(10, 123);
  cout << "交换前:" << endl;
  cout << "L1: ";
  PrintList(L1);
  cout << endl;
  cout << "L2: ";
  PrintList(L2);
  cout << endl;
  cout << "交换后: " << endl;
  L2.swap(L1);
  cout << "L1: ";
  PrintList(L1);
  cout << endl;
  cout << "L2: ";
  PrintList(L2);
  cout << endl;
}
int main()
{
  cout << "赋值:" << endl;
  test01();
  cout << "交换:\n" << endl;
  test02();
  system("pause");
  return 0;
}

运行结果:

list大小操作

功能:对list容器的大小进行操作

函数原型:

  • size();// 返回容器中元素的个数
  • empty();// 判断容器是否为空
  • resize(num);// 重新指定容器的长度为num,若容器变长,则以默认值填充新位置
    // 如果容器变短,则末尾超出容器的长度的元素被删除
  • resize(num, elem);// 重新指定容器的长度num,若容器变长,则以elem值填充新位置
    // 如果容器变短,则末尾超出容器长度的元素被删除
#include<iostream>
#include<list>
using namespace std;
// List container size operation 
void PrintList(const list<int>& L)
{
  for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
  {
    cout << *it << " ";
  }
  cout << endl;
}
void test01()
{
  list<int>L1;
  L1.push_back(10);
  L1.push_back(20);
  L1.push_back(30);
  L1.push_back(40);
  cout << "L1: ";
  PrintList(L1);
  cout << endl;
  // Judge whether the container is empty
  if (L1.empty())
  {
    cout << "L1为空" << endl;
  }
  else
  {
    cout << "L1不为空" << endl;
    cout << "L1的元素个数为:" << L1.size() << endl;
  }
  cout << endl;
  // Re size
  L1.resize(10, 10000); 
  cout << "L1: ";
  PrintList(L1);
  cout << endl;
  L1.resize(2);  // Take the first two elements of the list
  cout << "L1: ";
  PrintList(L1);
  cout << endl;
}
int main()
{
  test01();
  return 0;
}

运行结果:

list插入和删除

  • 尾插 — push_back();
  • 尾删 — pop_back();
  • 头插 — push_front();
  • 头删 — pop_front();
  • 插入 — insert();
  • 删除 — erase();
  • 移除 — remove();
  • 清空 — clear();

这里要说明的是list迭代器不能跳跃移动,例如insert函数中的参数写成L.begin() + n的操作是不可以的,只能写成++L.begin(), 或者提前定义一个迭代器变量it然后++it才能访问下一个位置,因为list的指针域指向指向下一个元素,并没有指向下下个元素。

What I want to explain here is that the list iterator cannot jump. For example, the operator of writing the parameter in the insert function as L.begin() + n is not allowed. it can only be written as ++L.begin(), or define an iterator variable “it” in advance, and then ++it can access the next position, because the pointer field of the list points to the next element, not to the next next element.

#include<iostream>
#include<list>
// Insertion and deletion of list container
using namespace std;
void PrintList(const list<int>&L)
{
  for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
  {
    cout << *it << " ";
  }
  cout << endl;
}
void test01()
{
  list<int>L;
  // push_back tail insertion
    // 尾插
  L.push_back(10);
  L.push_back(20);
  L.push_back(30);
  // push_front head insertion
    // 头插入
  L.push_front(100);
  L.push_front(200);
  L.push_front(300);
  // 300 200 100 10 20 30 
  cout << "L: ";
  PrintList(L);
  cout << endl;
  // pop_back tail deletion 
    // 头插
  L.pop_back();
  // 300 200 100 10 20 
  cout << "L: ";
  PrintList(L);
  cout << endl;
  // pop_head head deletion
    // 头插
  L.pop_front();
  // 200 100 10 20 
  cout << "L: ";
  PrintList(L);
  cout << endl;
  // Inser Insertion 
    // 索引为1的位置插入
  L.insert(++L.begin(), 12345);
  //  200 12345 100 10 20
  cout << "L: ";
  PrintList(L);
  cout << endl;
  // erase deletion 
    // 删掉索引为1的
  list<int>::const_iterator it = L.begin();
  L.erase(++it);
  // 200 100 10 20;
  cout << "L: ";
  PrintList(L);
  cout << endl;
  // Remove by value
    // 尾插
  L.push_back(10);
  // 200 100 10 20 10
  L.remove(10);
  // 200 100 20
  cout << "L: ";
  PrintList(L);
  cout << endl;
}
int main()
{
  test01();
  return 0;
}

运行结果:

list 数据存取

功能描述:对list容器中数据进行存取

要说明的是因为链表存储不是一段连续线性的空间,list容器不支持直接用at,[],两种方式的访问元素和随机访问,list的迭代器是一个双向迭代器,只支持前移或者后移,不支持跳跃式访问,所以只有front(), back()两个接口,不过可以通过’++'运算符,多加几次找到自己想要的位置,或者自己写个函数,函数里面的原理类似打印时的那个循环,改变迭代器的位置,来达到自己想要的位置.

It should be noted that because the list storage is not a continuous linear space. the list container does not support direct access to elements and random access by at and [] methods.

The iterator of the list is a two-way iterator, which only supports forward or backward movement, and does not support skip access, so it has only two functions front() and back() are two interfaces, but you can use the '++'operator to add several times to find the position

you want, or you can write a function. The principle of the function is similar to the loop when printing, and you can change the position of the iteration to achieve the position you want.

函数原型:

  • front();// 返回第一个元素
  • back();// 返回最后一个元素
#include<iostream>
#include<list>
using namespace std;
/*Data access of list container*/
void PrintList(const list<int>& L)
{
  for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
  {
    cout << *it << " ";
  }
  cout << endl;
}
void test01()
{
  list<int>L1;
  L1.push_back(10);
  L1.push_back(20);
  L1.push_back(30);
  L1.push_back(40);
  cout << "第一个元素为:" << L1.front() << endl;
  cout << "最后一个元素为:" << L1.back() << endl;
  // Verification iterators do not support random access
  list<int>::iterator it = L1.begin();
  // it += 1; it += 5; It's all wrong
}
int main()
{
  test01();
  system("pause");
  return 0;
}

运行结果:

list反转和排序

功能描述:将容器中的元素翻转,以及将容器中的数据排序。

函数原型:

  • reverse();// 反转链表
  • sort();// 链表排序

注意:所有不支持随机访问的迭代器的容器,不可以用标准的排序算法。

不支持随机访问迭代器的容器,内部会提供对应的一些算法.

Note: all containers that do not support random access iterators cannot use standard sorting algorithm.

Containers that do not support random access iterators will provide corresponding algorithm.

#include<iostream>
#include<list>
#include<algorithm>
using namespace std;
/*Reverse and sort of list container*/
bool cmp(int v1, int v2)
{
  return v1 > v2;
}
void PrintList(const list<int>& L)
{
  for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
  {
    cout << *it << " ";
  }
  cout << endl;
}
void test01()
{
  list<int>L1;
  L1.push_back(20);
  L1.push_back(10);
  L1.push_back(50);
  L1.push_back(40);
  L1.push_back(0);
  // Reverse 
  cout << "反转前L1: ";
  PrintList(L1);
  cout << endl;
  cout << "反转后L1: ";
  L1.reverse();
  PrintList(L1);
  cout << endl;
}
void test02()
{
  list<int>L1;
  L1.push_back(20);
  L1.push_back(10);
  L1.push_back(50);
  L1.push_back(40);
  L1.push_back(0);
  // Sort
  // You can't write that
  /*
  sort(L1.begin(), L1.end());
  */
  L1.sort();  // Default rule: ascending order, from small to large
  cout << "升序 排序后L1: ";
  PrintList(L1);
  cout << endl;
  L1.sort(cmp);  // You can customize function rules
  cout << "降序 排序后L1: ";
  PrintList(L1);
  cout << endl;
}
int main()
{
  test01();
  test02();
  system("pause");
  return 0;
}

运行结果:

相关文章
|
存储 Java 索引
(Python基础)新时代语言!一起学习Python吧!(二):字符编码由来;Python字符串、字符串格式化;list集合和tuple元组区别
字符编码 我们要清楚,计算机最开始的表达都是由二进制而来 我们要想通过二进制来表示我们熟知的字符看看以下的变化 例如: 1 的二进制编码为 0000 0001 我们通过A这个字符,让其在计算机内部存储(现如今,A 字符在地址通常表示为65) 现在拿A举例: 在计算机内部 A字符,它本身表示为 65这个数,在计算机底层会转为二进制码 也意味着A字符在底层表示为 1000001 通过这样的字符表示进行转换,逐步发展为拥有127个字符的编码存储到计算机中,这个编码表也被称为ASCII编码。 但随时代变迁,ASCII编码逐渐暴露短板,全球有上百种语言,光是ASCII编码并不能够满足需求
540 4
|
存储 缓存 C++
C++ 容器全面剖析:掌握 STL 的奥秘,从入门到高效编程
C++ 标准模板库(STL)提供了一组功能强大的容器类,用于存储和操作数据集合。不同的容器具有独特的特性和应用场景,因此选择合适的容器对于程序的性能和代码的可读性至关重要。对于刚接触 C++ 的开发者来说,了解这些容器的基础知识以及它们的特点是迈向高效编程的重要一步。本文将详细介绍 C++ 常用的容器,包括序列容器(`std::vector`、`std::array`、`std::list`、`std::deque`)、关联容器(`std::set`、`std::map`)和无序容器(`std::unordered_set`、`std::unordered_map`),全面解析它们的特点、用法
1221 1
C++ 容器全面剖析:掌握 STL 的奥秘,从入门到高效编程
|
算法 C++ 容器
模拟实现c++中的list模版
模拟实现c++中的list模版
|
存储 安全 编译器
c++入门
c++作为面向对象的语言与c的简单区别:c语言作为面向过程的语言还是跟c++有很大的区别的,比如说一个简单的五子棋的实现对于c语言面向过程的设计思路是首先分析解决这个问题的步骤:(1)开始游戏(2)黑子先走(3)绘制画面(4)判断输赢(5)轮到白子(6)绘制画面(7)判断输赢(8)返回步骤(2) (9)输出最后结果。但对于c++就不一样了,在下五子棋的例子中,用面向对象的方法来解决的话,首先将整个五子棋游戏分为三个对象:(1)黑白双方,这两方的行为是一样的。(2)棋盘系统,负责绘制画面。
295 0
|
存储 分布式计算 编译器
C++入门基础2
本内容主要讲解C++中的引用、inline函数和nullptr。引用是变量的别名,与原变量共享内存,定义时需初始化且不可更改指向对象,适用于传参和返回值以提高效率;const引用可增强代码灵活性。Inline函数通过展开提高效率,但是否展开由编译器决定,不建议分离声明与定义。Nullptr用于指针赋空,取代C语言中的NULL。最后鼓励持续学习,精进技能,提升竞争力。
|
算法 网络安全 区块链
2023/11/10学习记录-C/C++对称分组加密DES
本文介绍了对称分组加密的常见算法(如DES、3DES、AES和国密SM4)及其应用场景,包括文件和视频加密、比特币私钥加密、消息和配置项加密及SSL通信加密。文章还详细展示了如何使用异或实现一个简易的对称加密算法,并通过示例代码演示了DES算法在ECB和CBC模式下的加密和解密过程,以及如何封装DES实现CBC和ECB的PKCS7Padding分块填充。
525 4
2023/11/10学习记录-C/C++对称分组加密DES
|
C++ 开发者
C++学习之继承
通过继承,C++可以实现代码重用、扩展类的功能并支持多态性。理解继承的类型、重写与重载、多重继承及其相关问题,对于掌握C++面向对象编程至关重要。希望本文能为您的C++学习和开发提供实用的指导。
351 16
|
算法 C语言 C++
【c++丨STL】list的使用
本文介绍了STL容器`list`的使用方法及其主要功能。`list`是一种双向链表结构,适用于频繁的插入和删除操作。文章详细讲解了`list`的构造函数、析构函数、赋值重载、迭代器、容量接口、元素访问接口、增删查改操作以及一些特有的操作接口如`splice`、`remove_if`、`unique`、`merge`、`sort`和`reverse`。通过示例代码,读者可以更好地理解如何使用这些接口。最后,作者总结了`list`的特点和适用场景,并预告了后续关于`list`模拟实现的文章。
675 7
|
编译器 C语言 C++
【c++丨STL】list模拟实现(附源码)
本文介绍了如何模拟实现C++中的`list`容器。`list`底层采用双向带头循环链表结构,相较于`vector`和`string`更为复杂。文章首先回顾了`list`的基本结构和常用接口,然后详细讲解了节点、迭代器及容器的实现过程。 最终,通过这些步骤,我们成功模拟实现了`list`容器的功能。文章最后提供了完整的代码实现,并简要总结了实现过程中的关键点。 如果你对双向链表或`list`的底层实现感兴趣,建议先掌握相关基础知识后再阅读本文,以便更好地理解内容。
508 2

热门文章

最新文章