黑马c++ STL部分 笔记(7) list容器

简介: 黑马c++ STL部分 笔记(7) list容器

list基本概念

功能:将数据进行链式存储

链表(list)是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接实现的

链表的组成:链表由一系列结点组成

结点的组成:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域

STL中的链表是一个双向循环链表

57765e179a17441f81172c7630058b03.jpg

由于链表的存储方式并不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器

list的优点

采用动态存储分配,不会造成内存浪费和溢出

链表执行插入和删除操作十分方便,修改指针即可,不需要移动大量元素

list的缺点

链表灵活,但是空间(指针域) 和 时间(遍历)额外耗费较大

List有一个重要的性质,插入操作和删除操作都不会造成原有list迭代器的失效,这在vector是不成立的。


总结:STL中List和vector是两个最常被使用的容器,各有优缺点

1.list构造函数

// list构造函数
/*
list<T> lst; //list采用采用模板类实现,对象的默认构造形式:
list(beg,end); //构造函数将[beg, end)区间中的元素拷贝给本身。
list(n,elem); //构造函数将n个elem拷贝给本身。
list(const list &lst); //拷贝构造函数。
*/
#include <bits/stdc++.h>
using namespace std;
void printlist(list<int> &l)
{
  for (list<int>::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);
  printlist(l1); // 10 20 30 40
  // 区间方式构造
  list<int> l2(l1.begin(), l1.end());
  printlist(l2); // 10 20 30 40
  list<int> l3;
  l3 = l2;
  printlist(l3);        // 10 20 30 40
  list<int> l4(3, 100); // n个element
  printlist(l4);        // 100 100 100
}
int main()
{
  test01();
}
/*
总结:
list构造方式同其他几个STL常用容器,熟练掌握即可
*/


2.list 赋值和交换

// list 赋值和交换
/*
list& operator=(const list &lst); //重载等号操作符
assign(beg, end); //将[beg, end)区间中的数据拷贝赋值给本身。
assign(n, elem); //将n个elem拷贝赋值给本身。
swap(lst); //将lst与本身的元素互换。
*/
#include <bits/stdc++.h>
using namespace std;
void printlist(list<int> &l)
{
  for (list<int>::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);
  printlist(l1); // 10 20 30 40
  list<int> l2;
  l2 = l1;       //=赋值
  printlist(l2); // 10 20 30 40
  list<int> l3;
  l3.assign(l2.begin(), l2.end());
  printlist(l3); // 10 20 30 40
  list<int> l4;
  l4.assign(3, 100); // n个element赋值
  printlist(l4);     // 100 100 100
  // 交换
  l1.swap(l4);
  printlist(l1);
  printlist(l4);
}
int main()
{
  test01();
}
/*
总结:
list赋值和交换操作能够灵活运用即可
*/


3.list 大小操作

// list 大小操作
/*
size(); //返回容器中元素的个数
empty(); //判断容器是否为空
resize(num); //重新指定容器的长度为num
若容器变长,则以默认值填充新位置。
若容器变短,则末尾超出容器长度的元素被删除。
resize(num, elem); //重新指定容器的长度为num
若容器变长,则以elem值填充新位置。
若容器变短,则末尾超出容器长度的元素被删除。
*/
#include <bits/stdc++.h>
using namespace std;
void printlist(list<int> &l)
{
  for (list<int>::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);
  printlist(l1); // 10 20 30 40
  if (l1.empty())
  {
    cout << "l1为空" << endl;
  }
  else
  {
    cout << "l1不为空" << endl;
    cout << "l1的元素个数为: " << l1.size() << endl; // 4
    l1.resize(6);
    printlist(l1); // 10 20 30 40 0 0
    l1.resize(7, 555);
    printlist(l1); // 10 20 30 40 0 0 555
  }
}
int main()
{
  test01();
}
/*
总结:
判断是否为空 — empty
返回元素个数 — size
重新指定个数 — resize
*/


4.list 插入和删除

// list 插入和删除
/*
push_back(elem);//在容器尾部加入一个元素
pop_back();//删除容器中最后一个元素
push_front(elem);//在容器开头插入一个元素
pop_front();//从容器开头移除第一个元素
insert(pos,elem);//在pos位置插elem元素的拷贝,返回新数据的位置。
insert(pos,n,elem);//在pos位置插入n个elem数据,无返回值。
insert(pos,beg,end);//在pos位置插入[beg,end)区间的数据,无返回值。
clear();//移除容器的所有数据
erase(beg,end);//删除[beg,end)区间的数据,返回下一个数据的位置。
erase(pos);//删除pos位置的数据,返回下一个数据的位置。
remove(elem);//删除容器中所有与elem值匹配的元素。
*/
#include <bits/stdc++.h>
using namespace std;
void printlist(list<int> &l)
{
  for (list<int>::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_front(100);
  l1.push_front(200);
  l1.push_front(300);
  printlist(l1); // 300 200 100 10 20 30
  // 尾删
  l1.pop_back(); // 300 200 100 10 20
  printlist(l1);
  // 头删
  l1.pop_front(); // 200 100 10 20
  printlist(l1);
  // insert插入
  list<int>::iterator it = l1.begin(); // list不能随机访问
  it++;                                // 不能it=it+2
  l1.insert(it, 1000);                 // 200 1000 100 10 20
  printlist(l1);
  it++;
  l1.insert(it, 1001); // 200 1000 100 1001 10 20
  printlist(l1);
  // erase删除
  it = l1.begin();
  l1.erase(it);
  printlist(l1); // 1000 100 1001 10 20
  // remove
  l1.push_back(1000);
  l1.remove(1000); // 删除容器中所有与elem值匹配的元素。
  printlist(l1);   // 100 1001 10 20
  // clear清空
  l1.clear();
  printlist(l1);
}
int main()
{
  test01();
}
/*
总结:
尾插 — push_back
尾删 — pop_back
头插 — push_front
头删 — pop_front
插入 — insert
删除 — erase
移除 — remove
清空 — clear
*/


5.list 数据存取

// list 数据存取
/*
front(); //返回第一个元素。
back(); //返回最后一个元素。
不能用[]和at,因为list不用连续的空间存储,不能随机访问
*/
#include <bits/stdc++.h>
using namespace std;
void test01()
{
  list<int> l1;
  l1.push_back(10);
  l1.push_back(20);
  l1.push_back(30);
  l1.push_back(40);
  // 不能用l1[]和l1.at()
  // 因为list本质上是链表,不是用连续线性空间存储数据,迭代器也是不支持随机访问的
  // it+1不行,it++可以;it-1不行,it--可以(支持双向)
  // 迭代器只能一个一个往后/前访问
  cout << l1.front() << " " << l1.back() << endl; // 10 40
}
int main()
{
  test01();
}
/*
总结:
list容器中不可以通过[]或者at方式访问数据
返回第一个元素 — front
返回最后一个元素 — back
*/


6.list 反转和排序

// list 反转和排序
/*
reverse(); //反转链表
sort(); //链表排序
*/
#include <bits/stdc++.h>
using namespace std;
void printlist(list<int> &l)
{
  for (list<int>::iterator it = l.begin(); it != l.end(); it++)
  {
    cout << *it << " ";
  }
  cout << endl;
}
bool cmp(int l1, int l2)
{ // 降序
  if (l1 > l2)
    return true;
  return false;
}
void test01()
{
  list<int> l1;
  l1.push_back(20);
  l1.push_back(10);
  l1.push_back(50);
  l1.push_back(40);
  l1.push_back(30);
  printlist(l1); // 20 10 50 40 30
  // 反转reverse
  l1.reverse();
  printlist(l1); // 30 40 50 10 20
  // sort(l1.begin(), l1.end());不对
  // 因为所有不支持随机访问迭代器的容器,不能用标准算法,其内部会提供相应算法
  l1.sort();     // 对 升序
  printlist(l1); // 10 20 30 40 50
  l1.sort(cmp);  // 降序
  printlist(l1); // 50 40 30 20 10
}
int main()
{
  test01();
}
/*
总结:
反转 — reverse
排序 — sort (成员函数)
*/


相关文章
|
2天前
|
算法 C++ 容器
模拟实现c++中的list模版
模拟实现c++中的list模版
|
2月前
|
编译器 C语言 C++
【c++丨STL】list模拟实现(附源码)
本文介绍了如何模拟实现C++中的`list`容器。`list`底层采用双向带头循环链表结构,相较于`vector`和`string`更为复杂。文章首先回顾了`list`的基本结构和常用接口,然后详细讲解了节点、迭代器及容器的实现过程。 最终,通过这些步骤,我们成功模拟实现了`list`容器的功能。文章最后提供了完整的代码实现,并简要总结了实现过程中的关键点。 如果你对双向链表或`list`的底层实现感兴趣,建议先掌握相关基础知识后再阅读本文,以便更好地理解内容。
42 1
|
2月前
|
算法 C语言 C++
【c++丨STL】list的使用
本文介绍了STL容器`list`的使用方法及其主要功能。`list`是一种双向链表结构,适用于频繁的插入和删除操作。文章详细讲解了`list`的构造函数、析构函数、赋值重载、迭代器、容量接口、元素访问接口、增删查改操作以及一些特有的操作接口如`splice`、`remove_if`、`unique`、`merge`、`sort`和`reverse`。通过示例代码,读者可以更好地理解如何使用这些接口。最后,作者总结了`list`的特点和适用场景,并预告了后续关于`list`模拟实现的文章。
69 7
|
2月前
|
存储 编译器 C++
C++ initializer_list&&类型推导
在 C++ 中,`initializer_list` 提供了一种方便的方式来初始化容器和传递参数,而右值引用则是实现高效资源管理和移动语义的关键特性。尽管在实际应用中 `initializer_list&&` 并不常见,但理解其类型推导和使用方式有助于深入掌握现代 C++ 的高级特性。
28 4
|
3月前
|
存储 设计模式 C++
【C++】优先级队列(容器适配器)
本文介绍了C++ STL中的线性容器及其适配器,包括栈、队列和优先队列的设计与实现。详细解析了`deque`的特点和存储结构,以及如何利用`deque`实现栈、队列和优先队列。通过自定义命名空间和类模板,展示了如何模拟实现这些容器适配器,重点讲解了优先队列的内部机制,如堆的构建与维护方法。
60 0
|
4月前
|
存储 算法 C++
【C++打怪之路Lv10】-- list
【C++打怪之路Lv10】-- list
33 1
|
4月前
|
存储 编译器 C++
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
103 2
|
4月前
|
存储 缓存 C++
C++番外篇——list与vector的比较
C++番外篇——list与vector的比较
37 0
|
4月前
|
C++
C++番外篇——list的实现
C++番外篇——list的实现
29 0
|
4月前
|
存储 C++ 容器
C++入门9——list的使用
C++入门9——list的使用
27 0