Day15 C++STL入门基础知识九——list容器 基本概念-构造函数-赋值变换-大小操作-插入删除-数据存取-反转排序 【全面深度剖析+例题代码展示】

1. 基本概念

1.1 功能

  • 将数据进行链式存储

1.2 概念

  • list是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接实现的。STL中的链表是一个双向循环链表

1.3 组成和存储方式

  • 链表的组成:链表由一系列结点组成
  • 结点的组成:是一个储存元素的数据域,另一个存储下一个结点地址的指针域
  • 链表的存储方式并不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器

1.4 优缺点

  • 优点:
    • 采用动态存储分配,不会造成内存的浪费和溢出
    • 方便插入和删除,直接修改指针即可,不需移动大量元素
    • 插入删除都不会造成原有list迭代器失效,这在vector是不成立的。
  • 缺点:
    • 链表灵活,但空间(指针域)和时间(遍历)额外耗费较大

1.4 图解

在这里插入图片描述
链表

2. 构造函数

2.1 函数原型

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

2.2 代码展示

#include<stdio.h>
#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;
	cout << "-----------------------------------------------" << endl;
}

void text01() {
    
    

	//①默认构造
	list<int> l1;	
	l1.push_back(10);
	l1.push_back(20);
	l1.push_back(30);
	l1.push_back(40);
	l1.push_back(50);
	printList(l1);

	//②区间方式构造
	list<int> l2(++l1.begin(), --l1.end());
	printList(l2);

	//③拷贝构造
	list<int> l3 = l2;
	printList(l3);

	//④n个elem
	list<int> l4(10, 5);
	printList(l4);

}


int main() {
    
    
	text01();
	return 0;
}

2.3 测试结果

在这里插入图片描述

3. 赋值交换

3.1 函数原型

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

3.2 代码展示

#include<stdio.h>
#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;
	cout << "-----------------------------------------------" << endl;
}

void printList2(const list<int> l) {
    
    
	for (list<int> ::const_iterator it = l.begin(); it != l.end(); it++) {
    
    
		cout << (*it) << " ";
	}
	cout << endl;
}
void text02() {
    
    
	list<int> lst1;

	lst1.push_back(10);
	lst1.push_back(20);
	lst1.push_back(30);
	lst1.push_back(40);
	lst1.push_back(50);
	printList(lst1);

	//=赋值
	list<int> lst2(lst1);
	printList(lst2);

	//assign赋值
	list<int> lst3;
	lst3.assign(++lst2.begin()  , lst2.end());
	printList(lst3);

	//n个ele赋值
	list<int> lst4;
	lst4.assign(10, 3);
	printList(lst4);
	cout << endl;

	//交换容器
	cout << "lst3="; printList2(lst3);
	cout << "lst4="; printList2(lst4);
	swap(lst3, lst4);
	cout << "lst3="; printList2(lst3);
	cout << "lst4="; printList2(lst4);

}

int main() {
    
    
	text02();
	return 0;
}

3.3 测试结果

在这里插入图片描述

4. 大小操作

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

1.3 代码展示

#include<stdio.h>
#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;
	cout << "-----------------------------------------------" << endl;
}
void text03() {
    
    
	list<int> l;
	l.push_back(10);
	l.push_back(20);
	l.push_back(30);
	l.push_back(40);
	l.push_back(50);
	
	//判空
	if (l.empty()) cout << "容器为空" << endl;
	else cout << "容器不为空" << endl;

	//容器大小
	cout << "容器大小为:" << l.size() << endl;

	//重新指定大小
	l.resize(10);
	printList(l);

	l.resize(4);
	printList(l);

	//重载重新指定大小
	l.resize(10, 5);
	printList(l);
}

int main() {
    
    
	text03();
	return 0;
}

1.4 测试结果

在这里插入图片描述

6. 插入删除

6.1 函数原型

  • push_back(); 尾插
  • pop_back(); 尾删
  • push_front(elem); 在容器开头插入一个元素
  • pop_front(elem); 在容器开头移除第一个元素
  • 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匹配的元素

6.2 代码展示

#include<stdio.h>
#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;
	cout << "-----------------------------------------------" << endl;
}
void text04() {
    
    
	list<int> l1;

	//尾插
	l1.push_back(10);
	l1.push_back(20);
	l1.push_back(30);
	printList(l1);

	//头插
	l1.push_front(300);
	l1.push_front(200);
	l1.push_front(100);
	printList(l1);

	//尾删
	l1.pop_back();
	printList(l1);

	//头删
	l1.pop_front();
	printList(l1);

	//insert插入
	l1.insert(l1.begin(), 8);
	printList(l1);

	//insert重载
	l1.insert(++l1.begin(), 1, 9);
	printList(l1);

	list<int>::iterator it = l1.begin();
	
	//删除
	it = l1.begin();
	l1.erase(++it);
	printList(l1);

	//匹配上的全部移除
	l1.push_back(10000);
	l1.push_back(10000);
	printList(l1);
	l1.remove(10000);
	printList(l1);

	//清空
	l1.clear();
	printList(l1);
}

int main() {
    
    
	text04();
	return 0;
}

6.3 测试结果

在这里插入图片描述

7. 数据存取

7.1 函数原型

  • front(); 返回第一个元素
  • back(); 返回最后一个元素

7.2 代码展示

#include<stdio.h>
#include<iostream>
#include<list>
using namespace std;
void text05() {
    
    
	list<int> l1;
	l1.push_back(10);
	l1.push_back(20);
	l1.push_back(30);
	l1.push_back(40);
	l1.push_back(50);

	cout << l1.front() << endl;
	cout << l1.back() << endl;

	list<int> ::iterator it = l1.begin();
	//链表不是用线性空间的存储数据,迭代器不能随机访问。
	it++;	it--;  //支持双向
	//it += 1;   报错 ==> 不支持随机访问
}


int main() {
    
    
	text05();
	return 0;
}

7.3 测试结果

在这里插入图片描述

8. 反转排序

8.1 函数原型

  • reverse(); 反转
  • sort(); 排序

8.2 代码展示


#include<algorithm>
bool cmp(int t1, int t2) {
    
    
	return t1 > t2;
}

void text06() {
    
    
	list<int> l1;
	l1.push_back(10);
	l1.push_back(2);
	l1.push_back(300);
	l1.push_back(50);
	l1.push_back(150);
	cout << "反转前:";  printList(l1);

	//反转
	l1.reverse();
	cout << "反转后:";  printList(l1);

	//排序
	//sort(l1.begin(), l1.end());
	//不支持随机访问迭代器的容器,内部会提供一些算法
	l1.sort(); 
	cout << "小->大排序后:";  printList(l1);
	l1.sort(cmp);
	cout << "大->小排序后:";  printList(l1);
}

int main() {
    
    
	text06();
	return 0;
}

8.3 测试结果

在这里插入图片描述

9. 自定义数据类型排序案例

9.1 题目

  • 案例描述:将Player自定义数据类型进行排序,Person中属性有姓名、年龄、得分。
  • 排序规则:按照年龄进行升序,如果年龄相同按照得分进行降序

9.2 代码展示

#include<stdio.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<list>
using namespace std;

class Player {
    
    
public:
	Player(string name, int age, int score) {
    
    
		this->m_name = name;
		this->m_age = age;
		this->m_score = score;
	}
public:
	string m_name;
	int m_age;
	int m_score;
};

bool cmp(Player &p1, Player &p2) {
    
    	
	if (p1.m_age != p2.m_age) return p1.m_age < p2.m_age;
	else return p1.m_score > p2.m_score;
}

void printList(const list<Player>& l) {
    
    
	for (list<Player>::const_iterator it = l.begin(); it != l.end(); it++) {
    
    
		cout << (*it).m_name << " " << (*it).m_age << " " << (*it).m_score << endl;
	}
	cout << endl;
}

void text01() {
    
    
	//创建容器
	list<Player> l1;
	//定义数据
	Player p1("James", 38, 4000);
	Player p2("Curry", 34, 3000);
	Player p3("Irving", 30, 3500);
	Player p4("Harden", 36, 3300);
	Player p5("Durant", 34, 3800);
	//插入数据
	l1.push_back(p1);
	l1.push_back(p2);
	l1.push_back(p3);
	l1.push_back(p4);
	l1.push_back(p5);
	//打印输出
	printList(l1);
	//排序
	l1.sort(cmp);
	//打印输出
	printList(l1);
}

int main() {
    
    
	text01();
	return 0;
}


9.3 测试结果

在这里插入图片描述

感谢大家支持u ^ _ ^

猜你喜欢

转载自blog.csdn.net/m0_73612212/article/details/128868164
今日推荐