黑马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 (成员函数)
*/


相关文章
|
3天前
|
存储 算法 C++
C++提高篇:泛型编程和STL技术详解,探讨C++更深层的使用
文章详细探讨了C++中的泛型编程与STL技术,重点讲解了如何使用模板来创建通用的函数和类,以及模板在提高代码复用性和灵活性方面的作用。
10 2
C++提高篇:泛型编程和STL技术详解,探讨C++更深层的使用
|
1月前
|
存储 算法 编译器
[C++] STL简介
[C++] STL简介
23 1
|
1月前
|
存储 算法 C++
C++ STL应用宝典:高效处理数据的艺术与实战技巧大揭秘!
【8月更文挑战第22天】C++ STL(标准模板库)是一组高效的数据结构与算法集合,极大提升编程效率与代码可读性。它包括容器、迭代器、算法等组件。例如,统计文本中单词频率可用`std::map`和`std::ifstream`实现;对数据排序及找极值则可通过`std::vector`结合`std::sort`、`std::min/max_element`完成;而快速查找字符串则适合使用`std::set`配合其内置的`find`方法。这些示例展示了STL的强大功能,有助于编写简洁高效的代码。
33 2
|
1月前
|
安全 编译器 容器
C++STL容器和智能指针
C++STL容器和智能指针
|
1月前
|
C++ 容器
C++中自定义结构体或类作为关联容器的键
C++中自定义结构体或类作为关联容器的键
34 0
|
9天前
|
编译器 C++
C++ 类构造函数初始化列表
构造函数初始化列表以一个冒号开始,接着是以逗号分隔的数据成员列表,每个数据成员后面跟一个放在括号中的初始化式。
55 30
|
24天前
|
存储 编译器 C++
C ++初阶:类和对象(中)
C ++初阶:类和对象(中)
|
1月前
|
存储 安全 编译器
【C++】类和对象(下)
【C++】类和对象(下)
【C++】类和对象(下)
|
24天前
|
C++
C++(十六)类之间转化
在C++中,类之间的转换可以通过转换构造函数和操作符函数实现。转换构造函数是一种单参数构造函数,用于将其他类型转换为本类类型。为了防止不必要的隐式转换,可以使用`explicit`关键字来禁止这种自动转换。此外,还可以通过定义`operator`函数来进行类型转换,该函数无参数且无返回值。下面展示了如何使用这两种方式实现自定义类型的相互转换,并通过示例代码说明了`explicit`关键字的作用。
|
24天前
|
存储 设计模式 编译器
C++(十三) 类的扩展
本文详细介绍了C++中类的各种扩展特性,包括类成员存储、`sizeof`操作符的应用、类成员函数的存储方式及其背后的`this`指针机制。此外,还探讨了`const`修饰符在成员变量和函数中的作用,以及如何通过`static`关键字实现类中的资源共享。文章还介绍了单例模式的设计思路,并讨论了指向类成员(数据成员和函数成员)的指针的使用方法。最后,还讲解了指向静态成员的指针的相关概念和应用示例。通过这些内容,帮助读者更好地理解和掌握C++面向对象编程的核心概念和技术细节。