C++容器篇-----list的使用方法

list的构造函数

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

//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> l;

	//添加数据
	l.push_back(10);
	l.push_back(20);
	l.push_back(30);
	l.push_back(40);

	//遍历容器
	printList(l);

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

	//拷贝构造函数
	list<int>l3(l2);
	printList(l);

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

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

 

list的赋值和交换

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

//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> l;

	//添加数据
	l.push_back(10);
	l.push_back(20);
	l.push_back(30);
	l.push_back(40);
	printList(l);

	//赋值

	list<int> l2;
	l2 = l;
	printList(l);

	list<int> l3;
	l3.assign(l2.begin(), l2.end());
	printList(l);

	list<int>l4;
	l4.assign(10, 100);
	printList(l);
}

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

	//添加数据
	l.push_back(10);
	l.push_back(20);
	l.push_back(30);
	l.push_back(40);

	//交换
	list<int> l2;
	l2.assign(10, 100);

	cout << "交换前: " << endl;
	printList(l);
	printList(l2);
	cout << endl;

	l.swap(l2);
	cout << "交换后: " << endl;
	printList(l);
	printList(l2);
	cout << endl;
}

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

 

list的大小操作

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

//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> l;

	//添加数据
	l.push_back(10);
	l.push_back(20);
	l.push_back(30);
	l.push_back(40);
	printList(l);

	//判断容器是否为空
	if (l.empty())
	{
		cout << "l为空" << endl;
	}
	else
	{
		cout << "l不空" << endl;
		cout << "l的元素个数: " << l.size() << endl;
	}

	//重新指定大小
	l.resize(10);
	printList(l);

	l.resize(2);
	printList(l);
}

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

 

list的插入和删除

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

//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> l;

	//尾插
	l.push_back(10);
	l.push_back(20);
	l.push_back(30);
	printList(l);

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

	printList(l);//输出  300 200 100 10 20 30

	//尾删
	l.pop_back();

	//头删
	l.pop_front();

	printList(l);//输出  200 100 10 20 

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

	printList(l);//输出  200 1000 100 10 20 

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

	//移除
	l.push_back(10000);
	l.push_back(10000);
	l.push_back(10000);
	printList(l);//输出  1000 100 10 20  10000 10000 10000
	l.remove(10000);
	printList(l);//输出  1000 100 10 20 

	//清空
	l.clear();
	printList(l);//输出  换行
}

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

 

list的数据存取

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

//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> l;

	//尾插
	l.push_back(10);
	l.push_back(20);
	l.push_back(30);
	printList(l);

	//l[1] 不可以用 [] 的方式访问list容器中的元素

	//l.at(1) 不可以用 at 的方式访问list容器中的元素

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

	cout << "第一个元素为: " << l.front() << endl;

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

	//验证迭代器是不支持随机访问的
	list<int>::iterator it = l.begin();
	it++;//支持双向
	it--;
	//it=it+1   报错   不支持随机访问   
}

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

 

list的反转和排序

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

//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> l;

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

	cout << "反转前: " << endl;
	printList(l);     //输出 10 20 30

	//反转后
	l.reverse();
	cout << "反转后: " << endl;
	printList(l);     //输出 30 20 10
}

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

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

	//尾插
	l.push_back(20);
	l.push_back(10);
	l.push_back(50);
	l.push_back(40);
	l.push_back(30);

	cout << "排序前: " << endl;
	printList(l);     //输出 20 10 50 40 30
	
	//所有不支持随机访问的迭代器的容器,不支持标准算法
	//不支持随机访问迭代器的容器,内部会提供对应的一些算法
	//sort(l.begin(), l.end());    ---- 这种写法是错误的

	l.sort();//默认规则  从小到大  升序
	cout << "排序后: " << endl;
	printList(l);     //输出 10 20 30 40 50

	l.sort(compare);  //降序
	printList(l);     //输出 50 40 30 20 10
}

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

	system("pause");
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值