Java 转 C++ 系列:STL容器之list

文章参考:黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难


文章目录

  • 一、list基本概念
  • 二、list使用方法
    • [2.1 list构造函数](#2.1 list构造函数)
    • [2.2 list 赋值和交换](#2.2 list 赋值和交换)
    • [2.3 list 大小操作](#2.3 list 大小操作)
    • [2.4 list 插入和删除](#2.4 list 插入和删除)
    • [2.5 list数据存取](#2.5 list数据存取)
    • [2.6 list 反转和排序](#2.6 list 反转和排序)
    • [2.7 迭代器输出](#2.7 迭代器输出)
    • [2.8 排序案例](#2.8 排序案例)

一、list基本概念

  链表 (list)是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接实现的,其由一系列 结点 组成,而结点则由存储数据元素的 数据域 和存储下一个结点地址的指针域 组成。STL中的链表是一个双向循环链表:

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

list的优点:

  • 采用动态存储分配,不会造成内存浪费和溢出
  • 链表执行插入和删除操作十分方便,修改指针即可,不需要移动大量元素

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

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

注:STL中 list 和 vector 是两个最常被使用的容器 ,各有优缺点,使用 list 要求添加头文件 #include<list>


二、list使用方法

2.1 list构造函数

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

示例代码:

cpp 复制代码
list<int> l1;
//l1 = { 10, 20, 30 };
l1.push_back(10);
l1.push_back(20);
l1.push_back(30);

list<int> l2(l1.begin(), l1.end());// 10 20 30

list<int> l3(l2);

list<int> l4(4, 100); // 4个100
// 100 100 100 100

另外,以下两种方法,是两种初始化方法:

cpp 复制代码
list<int> l;      // 无参构造,创建空链表
l = {10,20,30};   // 赋值操作 → 调用 operator=

list<int> l = {10,20,30};  // 初始化列表构造
list<int> l{10,20,30};     // 等价简写,纯构造

2.2 list 赋值和交换

cpp 复制代码
assign(beg, end);                        , 将[beg, end)区间中的数据拷贝赋值给本身。
assign(n, elem);                         , 将n个elem拷贝赋值给本身。
list& operator=(const list &lst);        , 重载等号操作符
swap(lst);                               , 将lst与本身的元素互换。

示例代码:

cpp 复制代码
list<int> l1 = { 1,2,3 };
list<int> l2 = l1; // 1 2 3 
list<int> l3;
l3.assign(l2.begin(), l2.end()); // 1 2 3
list<int> l4;
l4.assign(3, 2); // 2 2 2
l1.swap(l4);
// l1: 2 2 2
// l4: 1 2 3

2.3 list 大小操作

cpp 复制代码
size();             , 返回容器中元素的个数
empty();            , 判断容器是否为空
resize(num);        , 重新指定容器的长度为num,若容器变长则以默认值填充新位置,若容器变短则末尾超出容器长度的元素被删除
resize(num, elem);  , 重新指定容器的长度为num,若容器变长则以elem值填充新位置,若容器变短则末尾超出容器长度的元素被删除

示例代码:

cpp 复制代码
list<int> l1 = { 1,2,3 };
list<int> l2 = l1; // 1 2 3 
list<int> l3;
l3.assign(l2.begin(), l2.end()); // 1 2 3 
list<int> l4;
l4.assign(3, 2); // 2 2 2
l1.swap(l4);
// l1: 2 2 2
// l4: 1 2 3

2.4 list 插入和删除

cpp 复制代码
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值匹配的元素。

示例代码:

cpp 复制代码
list<int> l;
//尾插
l.push_back(10);
l.push_back(20);
l.push_back(30); // 10 20 30
//头插
l.push_front(100);
l.push_front(200);
l.push_front(300); // 300 200 100 10 20 30

//尾删
l.pop_back(); // 300 200 100 10 20

//头删
l.pop_front(); // 200 100 10 20

//插入
list<int>::iterator it = l.begin();
l.insert(++it, 1000); // ++it 让迭代器向后移动一位
// 200 1000 100 10 20

//删除
it = l.begin();
l.erase(++it); // ++it 让迭代器向后移动一位
// 200 100 10 20

//移除
l.remove(100); // 200 10 20
   
//清空
l.clear();

2.5 list数据存取

cpp 复制代码
front();       , 返回第一个元素。
back();        , 返回最后一个元素。

示例代码:

cpp 复制代码
list<int> l = { 1,2,3 };

//cout << l.at(0) << endl;//错误 不支持at访问数据
//cout << l[0] << endl; //错误  不支持[]方式访问数据
cout << "第一个元素为: " << l.front() << endl;
cout << "最后一个元素为: " << l.back() << endl;

//list容器的迭代器是双向迭代器,不支持随机访问
list<int>::iterator it = l.begin();
//it = it + 1;//错误,不可以跳跃访问,即使是+1

2.6 list 反转和排序

cpp 复制代码
reverse();    , 反转链表
sort();       , 链表排序

示例代码:

cpp 复制代码
#include <queue>
#include <iostream>
#include <list>
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;
}

bool myCompare(int val1, int val2) {
	return val1 > val2;
}

int main() {
	system("chcp 65001> nul");

	list<int> l = { 34,5,4,1, 6 };
	l.reverse(); // 6 1 4 5 34
	printList(l);

	l.sort(); // 默认从小到大
	// 1 6 4 5 34
	printList(l);

	// 自定义规则,倒叙
	l.sort(myCompare); // 34 6 5 4 1
	printList(l);
	return 0;
}

2.7 迭代器输出

list<int>& l 为例:

cpp 复制代码
void printList(const list<int>& l) {
	for (list<int>::const_iterator it = l.begin(); it != l.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

2.8 排序案例

描述:将Person自定义数据类型进行排序,Person中属性有姓名、年龄、身高

规则:按照年龄进行升序,如果年龄相同按照身高进行降序

cpp 复制代码
#include <queue>
#include <iostream>
#include <list>
using namespace std;

class Person {
public:
	Person(string name, int age, int height) : m_Name(name), m_Age(age), m_Height(height) {}
public:
	string m_Name;
	int m_Age;
	int m_Height;
};
void printList(const list<Person>& l) {
	for (list<Person>::const_iterator it = l.begin(); it != l.end(); it++) {
		cout << "姓名: " << it->m_Name << " 年龄: " << it->m_Age
			<< " 身高: " << it->m_Height << endl;
	}
	cout << endl;
}

bool myCompare(Person p1, Person p2) {
	return p1.m_Age != p2.m_Age ? p1.m_Age < p2.m_Age : p1.m_Height < p2.m_Height;
}

int main() {
	system("chcp 65001> nul");
	Person p1("刘备", 35, 175);
	Person p2("曹操", 45, 180);
	Person p3("孙权", 40, 170);
	Person p4("赵云", 25, 190);
	Person p5("张飞", 35, 160);
	Person p6("关羽", 35, 200);

	list<Person> l1;
	l1.push_back(p1);
	l1.push_back(p2);
	l1.push_back(p3);
	l1.push_back(p4);
	l1.push_back(p5);
	l1.push_back(p6);

	l1.sort(myCompare);
	printList(l1);
	return 0;
}

相关推荐
秋雨梧桐叶落莳9 小时前
iOS——抽屉视图详解
开发语言·macos·ui·ios·objective-c·cocoa
郝学胜-神的一滴9 小时前
Qt 高级开发 016:半内存管理机制
开发语言·c++·qt·程序人生·用户界面
会编程的土豆9 小时前
Go 语言匿名函数详解
c++·golang·xcode
一 乐9 小时前
在线考试|基于Springboot的在线考试管理系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·毕设·在线考试管理系统
Byte Wizard9 小时前
动态内存管理
c语言·开发语言
zzzsde9 小时前
【Linux】线程同步和互斥(5):线程池的实现&&线程安全
linux·运维·服务器·开发语言·算法·安全
会编程的土豆9 小时前
Go 语言闭包(Closure)详解
c++·golang·xcode
无忧.芙桃9 小时前
C语言文件操作
c语言·开发语言
月落归舟9 小时前
Java并发容器与框架
java·开发语言
右耳朵猫AI9 小时前
Golang技术周刊 2026年第20周
开发语言·后端·golang