(C++)list 容器基础知识

3.7 list 容器

3.7.1 list 基本概念

功能:将数据进行链式存储。

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

链表的组成:
链表由一系列结点组成。

结点的组成:
一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。

STL中的链表是一个双向循环链表

在这里插入图片描述

由于链表的存储方式并不是连续的内存空间,因此链表list中的迭代器只支持
前移和后移,属于双向迭代器

在这里插入图片描述

list 的优点:
	采用动态存储分配,不会造成内存浪费和溢出。
	链表执行插入和删除操作十分方便,修改指针即可,不需要移动大量元素。

list 的缺点:
	链表灵活,但是空间(指针域)和时间(遍历)额外耗费较大
	list 有一个重要的性质,插入操作的删除操作都不会造成原有list迭代器的失效,
	这在vector是不成立的。

总结:STL中list和vector是两个最常被使用的容器,各有优缺点。

3.7.2 list 构造函数


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

—————————————————————————————————————————————————————————————————————————————————————————————

#include <iostream>
#include <string>
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<int>l1;
	
	l1.push_back(10);
	l1.push_back(3);
	l1.push_back(5);

	printList(l1);

	list<int>l2(l1.begin(), l1.end());
	printList(l2);

	list<int>l3(l2);
	printList(l3);

	list<int>l4(10,1000);
	printList(l4);
}
int main()
{
	test01();
	system("pause");
	return 0;
}

3.7.3 list 赋值和交换


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

—————————————————————————————————————————————————————————————————————————————————————————————
#include <iostream>
#include <string>
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<int>L1;

	L1.push_back(1);
	L1.push_back(2);
	L1.push_back(3);
	L1.push_back(4);

	printList(L1);

	list<int>L2;
	L2 = L1;
	//内置重载 " = " 赋值

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

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

}
void test02()
{
	list<int>L1;

	L1.push_back(1);
	L1.push_back(2);
	L1.push_back(3);
	L1.push_back(4);

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

	cout << "BeforeSwap" << endl;
	printList(L1);
	printList(L2);

	L1.swap(L2);
	cout << "AfterSwap " << endl;
	printList(L1);
	printList(L2);
}
int main()
{
	test02();
	system("pause");
	return 0;
}

3.7.4 list 大小操作


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

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

	L1.push_back(1);
	L1.push_back(2);
	L1.push_back(3);
	L1.push_back(4);

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

	L1.resize(10,1000);
	printList(L1);

	L1.resize(2);
	printList(L1);
}
int main()
{
	test02();
	system("pause");
	return 0;
}

3.7.5 list 插入和删除

	
	push_back(elem);				//在容器尾部加入-一个元素
	pop_back();						//删除容器中最后- -个元素
	push_front(elem);				//在容器开头插入一个元素
	pop_front();					//从容器开头移除第一一个元素
	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值匹配的元素。
	
—————————————————————————————————————————————————————————————————————————————————————————————

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

	L.push_back(1);
	L.push_back(2);
	L.push_back(3);
	L.push_back(4);

	L.push_front(10);
	L.push_front(20);
	L.push_front(30);

	printList(L);
	//30 20 10 1 2 3 4

	//尾删
	//30 20 10 1 2 3 
	L.pop_back();

	//头删
	//20 10 1 2 3 
	L.pop_front();
	printList(L);

	//insert插入
	list<int>::iterator it = L.begin();
	L.insert(++it, 1000);
	printList(L);

	//删除
	it = L.begin();
	L.erase(++it);
	//20 10 1 2 3 
	printList(L);

	//移除
	L.push_back(10000);
	L.push_back(10000);
	printList(L);
	//20 10 1 2 3 10000 10000
	L.remove(10000);
	printList(L);
	//20 10 1 2 3 

	//清空
	L.clear();
	printList(L);
}	
int main()
{
	test02();
	system("pause");
	return 0;
}

3.7.6 list 数据存取

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

void test02()
{
	list<int>L;

	L.push_back(1);
	L.push_back(2);
	L.push_back(3);
	L.push_back(4);

	//L[0] 不可以用这种形式访问list容器中的元素
	//L.at(0) 也不可以
	//原因是list本质是链表,不是用连续线性空间存储数据,迭代器也是不支持随机访问的

	//输出链表的第一个和最后一个元素
	cout << L.front() << endl;
	cout << L.back() << endl;

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

	//支持双向
	it++;
	it--;
}	
int main()
{
	test02();
	system("pause");
	return 0;
}
	
	总结:
	list容器中不可以通过[]或者at方式访问数据
	返回第一个元素		————		front
	返回最后一个元素		————		back


3.7.7 list 反转和排序


	函数原型:
		reverse();	//反转链表
		sort();		//链表排序

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

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;

	L.push_back(4);
	L.push_back(3);
	L.push_back(2);
	L.push_back(5);
	L.push_back(1);
	
	cout<< "Before Reverse :" << endl;
	printList(L);

	//反转
	cout<< "After Reverse :" << endl;
	L.reverse();
	printList(L);

}
//通过返回一个bool型来从大到小,降序排列
bool myCompare(int INT1,int INT2)
{
	return INT1 > INT2;
}
void test02()
{
	list<int>L;

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

	//排序
	cout << "Before Sort" << endl;
	printList(L);

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

	//通过返回一个bool型来从大到小,降序排列
	L.sort(myCompare);
	printList(L);
}


int main()
{
	test02();
	system("pause");
	return 0;
}

3.7.8 list 基础案例——排序

案例描述:将Person自定义数据类型进行排序,Person中属性有姓名、年龄、身高
排序规则:按照年龄进行升序,如果年龄相同按照身高进行降序
#include <iostream>
#include <string>
using namespace std;
#include <list>
#include <algorithm>

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

bool myCompare(Person &p1, Person &p2)
{
	//年龄相同的情况下按身高降序排序
	if(p1.m_Age == p2.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);
	
	//排序前
	cout << "Before Sort" << endl;
	for (list<Person>::iterator it = L.begin(); it != L.end(); it++)
	{
		cout << "Name:" <<(*it).m_Name << "     ";
		cout << "Age:" <<(*it).m_Age << "     ";
		cout << "Weight:" << (*it).m_Height << endl;
	}

	//排序后
	cout << "After Sort" << endl;
	L.sort(myCompare);
	for (list<Person>::iterator it = L.begin(); it != L.end(); it++)
	{
		cout << "Name:" <<(*it).m_Name << "     ";
		cout << "Age:" <<(*it).m_Age << "     ";
		cout << "Weight:" << (*it).m_Height << endl;
	}

}

int main()
{
	test01();
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值