2021-8-17 C++提升学习 -> 3.7、list容器

目录

3.7.1 基本概念

3.7.2 构造函数

3.7.3复制和交换

3.7.4 大小操作

3.7.5 插入和删除

3.7.6数据存取

3.7.7反转和排序

3.7.8排序案例


3.7.1 基本概念

详情见数据结构中链表

并且在STL库中链表是双向循环的

3.7.2 构造函数

#include <iostream>
#include <string>
#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;
}

void test01()
{
	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个elm
	list<int>L4(10, 1000);
	printList(L4);

}

int main() {

	test01();
	system("pause");
}

3.7.3复制和交换

assign 和 swap

#include <iostream>
#include <string>
#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;
}

void test01()
{
	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;
	printList(L2);

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

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

//交换
void test02()
{
	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;
	cout << "L1:";
	printList(L1); cout << endl;
	cout << "L2:";
	printList(L2); cout << endl;

	L1.swap(L2);
	cout << "交换后:" << endl;
	cout << "L1:";
	printList(L1); cout << endl;
	cout << "L2:";
	printList(L2); cout << endl;
}
int main() {
	test02();
	test01();
	system("pause");
}

3.7.4 大小操作

#include <iostream>
#include <string>
#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;
}

void test01()
{
	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, 10000);
	printList(L1);

	L1.resize(2);
	printList(L1);


}
int main() {

	test01();

	system("pause");
}

3.7.5 插入和删除

尾插—— push_back         尾删—— pop_back

头插——push_front         头删—— pop_front

插入—— insert                 删除——erase

 移除—— remove              清空——clear

#include <iostream>
#include <string>
#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;
}

void test01()
{
	list<int>L1;
	
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

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

	printList(L1);

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

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

	//插入
	L1.insert(L1.begin(), 1000);
	printList(L1);

	list<int>::iterator it = L1.begin();
	L1.insert(++it, 1000);
	printList(L1);

	//删除
	it = L1.begin();
	L1.erase(it);
	printList(L1);
	
	//移除
	L1.push_back(10000);
	printList(L1);

	L1.remove(10000);//多个时删除容器中所有与这个数匹配的元素
	printList(L1);

	//清空
	L1.clear();
	printList(L1);

}

int main() {

	test01();
	system("pause");
}

3.7.6数据存取

list本质时链表,不是用连续的线性空间存储数据,迭代器也是不支持随机访问的

返回第一个元素:front

返回最后一个元素:back

#include <iostream>
#include <string>
#include<list>
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[10]不可以用【】方式访问容器中的元素

	//不可以用at的方式访问容器中的元素

	//原因:list本质时链表,不是用连续的线性空间存储数据,迭代器也是不支持随机访问的

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

	//验证迭代器是不支持随机访问的
	list<int>::iterator it = L1.begin();

	it++;//支持双向
	it--;

	//it = it + 1不支持随机访问
}

int main() {


	system("pause");
}

3.7.7反转和排序

#include <iostream>
#include <string>
#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 v1,int v2)
{
	//降序 就让第一个数大于第二个数
	return v1 < v2;
}
void test01()
{
	//反转
	list<int>L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);
	printList(L1);

	L1.sort();
	printList(L1);

	L1.reverse();
	printList(L1);

	L1.sort(myCompare);
	printList(L1);
}

int main() {

	test01();
	system("pause");
}

3.7.8排序案例

#include <iostream>
#include <string>
#include<list>
using namespace std;

class person 
{
public:
	person(string name, int age, int height)
	{
		this->m_Age = age;
		this->m_Height = height;
		this->m_Name = name;
	}



	int m_Age;//年龄
	int m_Height;//身高
	string m_Name;//姓名

};

//指定排序规则
bool comparePerosn(person &p1, person &p2)
{
	if (p2.m_Age == p1.m_Age)
	{
		//年龄相同 按照身高降序
		return p1.m_Height > p2.m_Height;
	}
	else
	{
		//按照年龄升序
		return p1.m_Age < p2.m_Age;
	}
}

void test01()
{
	list<person>L;//创建容器

	//准备数据
	person p1("刘备", 35, 175);
	person p2("曹操", 45, 180);
	person p3("孙权", 40, 170);
	person p4("赵云", 25, 190);
	person p5("张飞", 35, 160);
	person p6("关羽", 35, 200);

	//插入数据
	L.push_back(p1);
	L.push_back(p2);
	L.push_back(p3);
	L.push_back(p4);
	L.push_back(p5);
	L.push_back(p6);

	for (list<person>::iterator it = L.begin(); it != L.end(); it++)
	{
		cout << "姓名:" << (*it).m_Name << " 年龄:" << it->m_Age << " 身高:" << it->m_Height;
		cout << endl;
	}

	//排序
	cout << "——————————————————" << endl;
	cout << "排序后" << endl;

	L.sort(comparePerosn);
	for (list<person>::iterator it = L.begin(); it != L.end(); it++)
	{
		cout << "姓名:" << (*it).m_Name << " 年龄:" << it->m_Age << " 身高:" << it->m_Height;
		cout << endl;
	}

}

int main() {

	test01();
	system("pause");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值