「 C++ STL库入门实例」list.cpp

#include<iostream>
#include<list>
#include<algorithm>
using namespace std;
void printList(const list<int>& v)
{
	for (list<int>::const_iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	//list容器构造函数
	//创建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);
}
void test02()
{
	list<int>L5;
	L5.push_back(10);
	L5.push_back(20);
	L5.push_back(30);
	L5.push_back(40);
	L5.push_back(50);
	printList(L5);
	list<int>L6=L5;
	printList(L6);
	list < int>L7;
	L7.assign(10, 100);
	printList(L7);
}
void test03()
{
	list<int>L8;
	L8.push_back(10);
	L8.push_back(20);
	L8.push_back(30);
	L8.push_back(40);

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

	cout << "交换前:" << endl;
	printList(L8);
	printList(L9);

	cout << "交换后:" << endl;
	L8.swap(L9);
	printList(L8);
	printList(L9);
	cout << endl;
}
void test04()
{
	//list容器大小操作
	list<int>L10;
	L10.push_back(10);
	L10.push_back(20);
	L10.push_back(30);
	L10.push_back(40); 

	printList(L10);

	//判断容器是否为空

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

	//重新指定大小

	L10.resize(10,100000);
	printList(L10);

	L10.resize(2);
	printList(L10);

}
void test05()
{
	//list容器的插入和删除
	list<int>L11;

	//尾插
	L11.push_back(10);
	L11.push_back(20);
	L11.push_back(30);

	//头插
	L11.push_front(100);
	L11.push_front(200);
	L11.push_front(300);

	printList(L11);

	//尾删
	L11.pop_back();
	//300 200 100 10 20
	printList(L11);
	//头删
	L11.pop_front();
	//200 100 10 20
	printList(L11);

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

	L11.insert(L11.begin(), 1000);
	printList(L11);

	//删除
	it = L11.begin();
	L11.erase(++it);
	printList(L11);
	
	
	//移除
	L11.remove(1000);
	printList(L11);

	//清空
	L11.clear();
	printList(L11);
}
void test06()
{
	list<int>L12;

	L12.push_back(10);
	L12.push_back(20);
	L12.push_back(30);

	//L12.[0]不可以用【】访问list容器中的元素
	//L12.at(0) 不可以用at方式访问list容器中的元素
	//原因list本质是链表,不是用连续性空间存储数据,迭代器也是不支持随机访问的。
	cout << "第一个元素为" << L12.front() << endl;
	cout << "最后一个元素为" <<L12.back() << endl;

	//验证迭代器不支持随机访问
	list<int>::iterator it = L12.begin();
	it++;
	cout << *it<<endl;
	//it = it + 1;不允许+数,只允许++ -- 说明迭代器不支持随机访问
}
bool myCompare(int v1,int v2)
{
	//降序 就让第一个数>第二个数
	return v1 > v2;
}
void test07()
{
	//反转
	list<int>L13;
	L13.push_back(20);
	L13.push_back(10);
	L13.push_back(50);
	L13.push_back(40);
	L13.push_back(20);
	cout << "反转前:" << endl;
	printList(L13);
	cout << "反转后:" << endl;
	L13.reverse();
	printList(L13);
	
	//sort(L13.begin(), L13.end());
	//所有不支持随机访问迭代器的容器,不可以用标准算法
	//不支持随机访问迭代器的容器,内部会提供对应一些算法
	cout << "升序排序后:" << endl;
	L13.sort();//默认排序规则 从小到大 升序
	printList(L13);
	cout << "降序排序后:" << endl;
	L13.sort(myCompare);
	printList(L13);
}
int main()
{
	test01();
	test02();
	test03();
	test04();
	test05();
	test06();
	test07();
	system( "pause");
	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值