C++list的使用:尾插、头插、insert、erase、reverse、sort等的介绍

news/2024/9/19 2:18:14 标签: c++, list

文章目录

  • 前言
  • 一、尾插、头插、insert、erase
  • 二、reverse、sort
  • 总结


前言

C++list的使用:尾插、头插、insert、erase、reverse、sort等的介绍


一、尾插、头插、insert、erase

#include <iostream>
#include <list>

using namespace std;


void test_list1()
{
	list<int> lt;

	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);
	lt.push_back(5);

	lt.push_front(10);
	lt.push_front(20);

	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;


	// 在第5个位置插入数据
	// vector v.insert(v.begin() + 5, val)
	list<int>::iterator it = lt.begin();
	for (size_t i = 0; i < 5; i++)
	{
		++it;
	}
	
	lt.insert(it, 66);

	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;


	it = find(lt.begin(), lt.end(), 3);
	if (it != lt.end())
	{
		lt.insert(it, 100);

		*it *= 2;
		// insert 完后it不失效
	}

	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;


	it = find(lt.begin(), lt.end(), 2);
	if (it != lt.end())
	{
		lt.erase(it);

		//*it *= 2; // 会强制报错
		// erase 后it 失效
	}

	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;


	// 删除所有的偶数
	it = lt.begin();
	while (it != lt.end())
	{
		if (*it % 2 == 0)
		{
			it = lt.erase(it);
		}
		else
		{
			++it;
		}
	}


	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;


}


int main()
{

	test_list1();

	return 0;
}

在这里插入图片描述

二、reverse、sort

void test_list02()
{
	list<int> lt;

	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);
	lt.push_back(5);

	lt.push_front(10);
	lt.push_front(20);

	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;


	reverse(lt.begin(), lt.end());

	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;

	lt.reverse();

	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;


	//sort(lt.begin(), lt.end());
	//for (auto e : lt)
	//{
	//	cout << e << " ";
	//}
	//cout << endl;


	lt.sort();

	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;

}

在这里插入图片描述


总结

C++list的使用:尾插、头插、insert、erase、reverse、sort等的介绍


http://www.niftyadmin.cn/n/5664868.html

相关文章

面了智谱大模型算法岗,效率贼高!

最近这一两周不少互联网公司都已经开始秋招提前批面试了。不同以往的是&#xff0c;当前职场环境已不再是那个双向奔赴时代了。求职者在变多&#xff0c;HC 在变少&#xff0c;岗位要求还更高了。 最近&#xff0c;我们又陆续整理了很多大厂的面试题&#xff0c;帮助一些球友解…

UDP实现组播发送端和接收端

发送端 #include<sys/socket.h> #include<stdio.h> #include<arpa/inet.h> #include<unistd.h> #include<string.h> int main() {int ret;int sfdsocket(AF_INET,SOCK_DGRAM,0);if(sfd-1)perror("socket error");struct in_addr addr…

vue 2表格滚动加载

自定义指令 // 表格下拉滚动加载更多 Vue.directive(loadmore, { bind(el, binding) { // 节流函数 const throttle (fn, wait 300) > { let inThrottle, lastFn, lastTime; return function() { const context this, args…

《C++魔法:零开销实现抽象工厂模式》

在 C的编程世界里&#xff0c;设计模式就像是一把把神奇的钥匙&#xff0c;能够打开高效、可维护代码的大门。其中&#xff0c;抽象工厂模式是一种非常强大的创建型设计模式&#xff0c;它允许我们创建一系列相关的对象&#xff0c;而无需指定它们的具体类。然而&#xff0c;在…

机器学习的网络们

机器学习的网络们 1. 线性神经网络1.1 线性回归1.2 softmax 回归 2. 多层感知机2.1 多层感知机单分类此处引入激活函数 多分类多隐藏层 3. 卷积神经网络3.1 卷积核多输入输出通道多输入多输出 3.2 池化层3.3 卷积神经网络&#xff08;LeNet 1998&#xff09; 4. 现代神经网络4.…

C语言中__attribute__((aligned(x)))的作用?

__attribute__((aligned(x))) 是一种用于指定变量或数据结构对齐方式的 GCC 扩展。它的作用是强制编译器将指定的变量或数据结构按照给定的字节数 x 对齐。 对齐的意义在于&#xff1a; 性能优化&#xff1a;在某些架构上&#xff0c; 数据必须按特定边界对齐才能高效访问。比…

如何提升用户留存?

1. 用户分层与用户场景 依据业务实际状况&#xff0c;对用户进行分层&#xff0c;并梳理各层用户的场景。以外卖产品为例&#xff0c;不同人群的需求和场景各异。唯有明确用户场景&#xff0c;方能知晓如何留住用户。 用户分层及用户场景找寻可借助营销工具进行专业标签化&a…

Django:解决配置缓存问题

前言 Django的默认缓存是存在内存当中&#xff0c;重启服务缓存就失效了。 文章选择用redis做缓存&#xff0c;需要预先安装redis数据库并启动服务。 安装&#xff1a;pip install django-redis。 代码实现 在项目中的setting.py中配置&#xff1a; # 缓存配置 CACHES …