STL学习2常用容器2.6list

 

1.循环双向链表(首尾相连,next指针指向后一个结点,prev指针指向前一个结点),存储单元非连续、非顺序,对于元素的插入与移除永远是常数时间

2.不能以普通指针作为迭代器(vector可以),其迭代器为Bidirectional iterator(双向迭代器)

3.删除 remove(elem)删除容器中所有与elem匹配的元素

4.反转 reverse,质变反转

5.排序 sort

   5.1algorithm中的标准sort只能用于迭代器为随机迭代器的容器排序,迭代器不为随机迭代器的容器用内部sort方法

   5.2 不带任何参数的sort方法默认是从小到大排序,带返回值为bool类型的函数参数sort方法可以自定义排序规则

   5.3对于自定义的数据类型,必须指定排序规则

   5.4 案例,给葫芦娃高级排序

6.测试程序

#include "pch.h"
#include <iostream>
#include<list>
#include<algorithm>
#include<string>
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>L;//默认构造
	list<int>L2(10, 2);//构造10个2的list
	list<int>L3(L2.begin(), L2.end());//区间复制构造

	//头插尾插
	list<int>L4;
	L4.push_back(10);
	L4.push_back(20);
	L4.push_back(30);
	L4.push_front(100);
	L4.push_front(200);
	L4.push_front(300);
	//正序遍历
	for (list<int>::iterator it = L4.begin(); it != L4.end(); it++)
		cout << *it << " ";
	cout << endl;
	//逆序遍历

	for (list<int>::reverse_iterator it = L4.rbegin(); it != L4.rend(); it++)
		cout << *it << " ";
	cout << endl;
	//插入
	L4.insert(L4.begin(),1000);//插入的参数是迭代器
	printList(L4);//1000 300 200 100 10 20 30

	//remove(elem)删除容器中所有与elem相同的元素
	L4.remove(300);
	printList(L4);//1000 200 100 10 20 30
}

void test02()
{
	list<int>L;
	L.push_back(10);
	L.push_back(20);
	L.push_back(30);
	L.push_front(100);
	L.push_front(200);
	L.push_front(300);

	list<int>L2;
	//assign接口,重新赋值
	L2.assign(10, 100);//L2为10个100
	printList(L2);
	L2.assign(L.begin(), L.end());//用区间方式给容器赋值
	printList(L2);

	//front,back,第一和最后一个元素
	cout << "L2 front = " << L2.front()<<endl;
	cout << "L2 back = " << L2.back() << endl;
}
/*
反转排序
*/
bool myCompare(int v1, int v2)
{
	return v1 > v2;
}
void test03()
{
	list<int>L;
	L.push_back(10);
	L.push_back(20);
	L.push_back(30);
	L.push_front(100);
	L.push_front(200);
	L.push_front(300);
	cout << "L: ";
	printList(L);
	//反转(质变)
	L.reverse();
	cout << "reverses L: ";
	printList(L);

	//排序
	//所有系统提供的标准算法,使用的容器提供的迭代器必须支持随机访问
	//不支持随机访问的迭代器的容器,内部会提供相应的算法的接口
	//sort(L.begin(), L.end());报错
	L.sort();//默认排序规则 从小到大
	cout << "默认排序:";
	printList(L);

	//修改排序规则 从大到小
	L.sort(myCompare);
	cout << "mycapre: ";
	printList(L);
	
}
//自定义数据类型必须指定排序规则
class Person
{
public:
	Person(string name, int age,int Heigh)
	{
		m_Name = name;
		m_Age = age;
		m_Heigh = Heigh;
	}
	string m_Name;
	int m_Age;
	int m_Heigh;
};

bool myComparePerson(Person&p1, Person&p2)
{
	//按年龄 升序
	//若年龄相同按照身高进行降序
	if (p1.m_Age == p2.m_Age)
		return p1.m_Heigh > p2.m_Heigh;
	return p1.m_Age < p2.m_Age;


}
void test04()
{
	list<Person>L;
	Person p1("大娃",30,170);
	Person p2("二娃",28,160);
	Person p3("三娃",20,150);
	Person p4("四娃",20,166);
	Person p5("五娃",20,165);
	Person p6("爷爷",90,180);
	Person p7("妖精", 999,999);
	L.push_back(p1);
	L.push_back(p2);
	L.push_back(p3);
	L.push_back(p4);
	L.push_back(p5);
	L.push_back(p6);
	L.push_back(p7);

	for (list<Person>::iterator it = L.begin(); it != L.end(); it++)
	{
		cout << "姓名:" << it->m_Name << " 年龄: " << it->m_Age 
			<<"身高: "<<it->m_Heigh<< endl;
	}
//	L.sort()报错。自定义数据不能使用默认排序
	L.sort(myComparePerson);
	cout << "排序后身高: " << endl;
	for (list<Person>::iterator it = L.begin(); it != L.end(); it++)
	{
		cout << "姓名:" << it->m_Name << " 年龄: " << it->m_Age
			<< "身高: " << it->m_Heigh << endl;
	}
}
int main()
{
	//test01();
	//test02();
	//test03();
	test04();
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值