C++ list (链表)容器

C++ list 链表

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

void printList(const list<int>& L)
{
	for (list<int>::const_iterator it = L.begin(); it !=L.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	//创建list容器
	list<int>L1;

	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	//遍历元素
	printList(L1);

	//区间方式构造
	list<int>L2(L1.begin(), L1.end());
	printList(L2);

	//拷贝构造
	list<int>L3(L2);
	printList(L3);

	//n个elem
	list<int>L4(10, 1000);
	printList(L4);
}

int main()
{
	test01();

	system("pause");
	return 0;
}
cpp 复制代码
#include<iostream>
using namespace std;
#include<list>

void printList(const list<int>& L)
{
	for (list<int>::const_iterator it = L.begin(); it !=L.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}



void test02()
{
	//创建list容器
	list<int>L1;

	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	printList(L1);

	list<int>L2;
	L2 = L1;		// operater =
	printList(L2);

	list<int>L3;
	L3.assign(L2.begin(),L2.end());
	printList(L3);


	list<int>L4;
	L4.assign(10,1000);
	printList(L4);

}


void test03()
{
	//创建list容器
	list<int>L1;

	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	list<int>L2;
	L2.assign(10, 1000);

	cout << "交换前:" << endl;
	printList(L1);
	printList(L2);

	L1.swap(L2);
	cout << "交换后:"<<endl;
	printList(L1);
	printList(L2);

}


int main()
{
	//test01();
	test02();
	test03();

	system("pause");
	return 0;
}
cpp 复制代码
#include<iostream>
using namespace std;
#include<list>

void printList(const list<int>& L)
{
	for (list<int>::const_iterator it = L.begin(); it !=L.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	//创建list容器
	list<int>L1;

	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	//遍历元素
	printList(L1);

	if (L1.empty()) {
		cout << "L1为空" << endl;
	}
	else {
		cout << "L1不为空" << endl;
		cout << "L1的元素个数为:"<<L1.size() << endl;
	}

	//重新指定大小
	L1.resize(10,1000);
	printList(L1);
	
	L1.resize(2);
	printList(L1);

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

void printList(const list<int>& L)
{
	if (L.empty()) {
		cout<<"为空"<<endl;
		return;
	}

	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	//创建list容器
	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);

	//300 200 100 10 20 30
	//遍历元素
	printList(L1);

	//尾删
	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();
	L1.insert(++it, 1000);
	//200 1000 100 10 20
	printList(L1);

	//删除
	it = L1.begin();
	L1.erase(it);
	//1000 100 10 20
	printList(L1);

	//移除
	L1.push_back(10000);
	L1.push_back(10000);
	L1.push_back(10000);
	L1.push_back(10000);
	printList(L1);
	L1.remove(10000);
	printList(L1);

	L1.clear();
	printList(L1);

}

int main()
{
	test01();

	return 0;
}
cpp 复制代码
#include<iostream>
using namespace std;
#include<list>

void printList(const list<int>& L)
{
	if (L.empty()) {
		cout<<"为空"<<endl;
		return;
	}

	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	//创建list容器
	list<int>L1;

	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	cout << "第一个元素为:" << L1.front() << endl;
	cout << "最后一个元素为:" << L1.back() << endl;

	//验证 iterater sss
	list<int>::iterator it = L1.begin();
	it++; //支持双向
	it--;
	//it=it+1 //不支持双向访问
}

int main()
{
	test01();

	return 0;
}
cpp 复制代码
#include<iostream>
using namespace std;
#include<list>
#include<algorithm>
void printList(const list<int>& L)
{
	if (L.empty()) {
		cout<<"为空"<<endl;
		return;
	}

	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

bool mycmp(int a,int b)
{
	return a>b;
}

//list 容器反转和排序

void test01()
{
	//创建list容器
	list<int>L1;

	L1.push_back(30);
	L1.push_back(50);
	L1.push_back(20);
	L1.push_back(10);
	L1.push_back(40);

	cout << "反转之前:" << endl;
	printList(L1);

	//反转
	L1.reverse();
	cout << "反转后" << endl;
	printList(L1);

	//所有不支持随机访问迭代器的容器,不可以用标准算法
	//不支持随机访问迭代器的容器,内部会提供对应一些算法
	//sort(L1.begin(), L1.end());
	
	L1.sort();		//默认排序规则  从小到大	升序
	cout << "排序后:" << endl;
	printList(L1);

	L1.sort(mycmp);
	cout << "排序后:" << endl;
	printList(L1);

}

int main()
{
	test01();

	return 0;
}
相关推荐
{⌐■_■}23 分钟前
【计网】认识跨域,及其在go中通过注册CORS中间件解决跨域方案,go-zero、gin
java·linux·开发语言·c++·中间件·golang·gin
代码程序猿RIP38 分钟前
【C语言干货】回调函数
c语言·开发语言·数据结构·c++·算法
刚入门的大一新生1 小时前
C++初阶-模板初阶
开发语言·数据结构·c++
刃神太酷啦1 小时前
排序--数据结构初阶(4)(C/C++)
c语言·数据结构·c++·算法·leetcode·深度优先·广度优先
胡八一1 小时前
vs2022解决 此项目需要MFC库。从visual studio安装程序(单个组件选项卡)为正在使用的任何工具和体系结构安装他们问题
c++·mfc·visual studio
.hopeful.1 小时前
基于QT的仿QQ音乐播放器
开发语言·c++·qt
是店小二呀2 小时前
【优选算法-二分查找】二分查找算法解析:如何通过二段性优化搜索效率
c++·算法
鑫—萍2 小时前
C++——入门基础
c语言·开发语言·c++·学习·算法
溟洵2 小时前
【C++ Qt】快速上手 显⽰类控件(Label、LCDNumber、ProcessBar、CalendarWidget)
开发语言·c++·qt
半青年3 小时前
单例模式:全局唯一性在软件设计中的艺术实践
java·c++·python·单例模式