list容器

1、构造函数

#include <list>

void print(const list<int>& L)
{
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test1()
{
	list<int> L1; //默认构造
	//添加数据
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	//遍历
	print(L1);

	//区间方式构造
	list<int> L2(L1.begin(), L1.end());
	print(L2);

	//拷贝构造
	list<int> L3(L2);
	print(L3);

	//n个elem的方式
	list<int> L4(10, 100);
	print(L4);
}

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

2、赋值和交换

#include <list>

void print(const list<int>& L)
{
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

//赋值
void test1()
{
	list<int> L1;
	
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	print(L1);

	list<int> L2;
	//=赋值
	L2 = L1;
	print(L2);

	list<int> L3;
	//assign赋值
	L3.assign(L2.begin(), L2.end());
	print(L3);

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

//交换
void test2()
{
	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, 100);
	cout << "交换前:" << endl;
	print(L1);
	print(L2);

	cout << "交换后:" << endl;
	L1.swap(L2);
	print(L1);
	print(L2);
}

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

3、大小操作

#include <list>

void print(const list<int>& L)
{
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

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

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

	//重新指定大小
	L1.resize(8, 1);
	print(L1);

	L1.resize(2);
	print(L1);
}

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

4、插入和删除

#include <list>

void print(const list<int>& L)
{
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test1()
{
	list<int> L;
	L.push_back(10);
	L.push_back(20);
	L.push_front(300);
	L.push_front(400);
	//400, 300, 10, 20
	print(L);

	//尾删
	L.pop_back();
	//400, 300, 10
	print(L);
	//头删
	L.pop_front();
	//300 10
	print(L);

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

	//删除
	it = L.begin();
	L.erase(++it);
	//300 10
	print(L);

	//移除
	L.push_back(10000);
	L.push_back(10000);
	L.push_back(10000);
	L.push_back(10000);
	//300 10 10000 10000 10000 10000
	print(L);

	L.remove(10000);
	//300 10
	print(L);

	//清空
	L.clear();
	print(L);
}

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


5、容器存取

#include <list>

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

	//L1[0];不可以用[]访问
	//L1.at(0);不可以用at访问,容器下标
	
	//原因是list是链表,不是用连续线性空间存储数据,迭代器也不支持随机访问
	cout << "第一个元素为:" << L1.front() << endl;
	cout << "第二个元素为:" << L1.back() << endl;
	
	//验证迭代器不支持随机访问
	list<int>::iterator it = L1.begin();
	it++ ;//支持双向, 不支持+1,+2等随机访问
	it--;
	

}

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


6、反转和排序

#include <list>
#include <algorithm>

void print(const list<int>& L)
{
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test1()
{
	list<int> L1;
	L1.push_back(20);
	L1.push_back(10);
	L1.push_back(50);
	L1.push_back(30);
	L1.push_back(40);
	cout << "反转前:" << endl;
	print(L1);

	//反转——首尾交换
	L1.reverse();
	cout << "反转后:" << endl;
	print(L1);

}

bool myCompare(int v1, int v2)
{
	//降序,就让第一个数大于第二个数
	return v1 > v2;
}

//排序
void test2()
{
	list<int> L1;
	L1.push_back(20);
	L1.push_back(10);
	L1.push_back(50);
	L1.push_back(30);
	L1.push_back(40);
	cout << "排序前:" << endl;
	print(L1);
	//所有不支持随机访问的迭代器容器,不可以用标准算法
	//不支持随机访问的迭代器容器,内部会提供一些对应算法
	//L1.sort(L1.begin(), L1.end()); (X)
	L1.sort();//默认升序
	cout << "排序后:" << endl;
	print(L1);

	L1.sort(myCompare); //降序
	print(L1);
}

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




全部评论

相关推荐

点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务