STL标准容器 (list)

案例 -评委打分

#include<iostream>
using namespace std;
#include"string"
#include<vector>
#include<deque>
#include<algorithm>
#include<ctime>
class Person
{
public:

	Person(string name, int score)
	{
		this->m_Name = name;
		this->m_Score = score;

	}

	string m_Name;
	int m_Score;

};



void createPerson(vector <Person>&v)
{
	string nameSeed = "ABCDE";
	for (int i = 0; i < 5; i++)
	{
		string name = "选手";
		name += nameSeed[i];

		int score = 0;
		Person p(name, score);

		//将创建的person对象 放入容器中
		v.push_back(p);

	}

}

//打分
void setScore(vector<Person>& v)
{
	for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)

	{
		//将评委的分数 放入到deque容器中
		deque<int>d;
		for (int i = 0; i < 10; i++)
		{
			int score = rand()%41 + 60;//60`100
			d.push_back(score);

		}

		/*cout << "选手: " << it->m_Name << "打分: " << endl;
		for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)
		{
			cout << *dit << " ";
		}
		cout << endl;*/


		//排序
		sort(d.begin(), d.end());

		//去除最值
		d.pop_back();
		d.pop_front();

		//取平均值

		int sum = 0;
		for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)
		{
			sum += *dit;//累加每个评委的分数
		}
		int avg = sum/d.size();

		//将平均分 赋值给选手身上
		it->m_Score = avg;


	}
}

void showScore(vector<Person>& v)
{
	for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << "姓名: " << it->m_Name << "平均分: " << it->m_Score << endl;
	}
}


int main()
{
	//随机数种子
	srand((unsigned int)time(NULL));


	//创建5名选手
	vector<Person>v;//存放在选手容器中

	createPerson(v);


	//2:给5名选手打分
	setScore(v);

	//显示最后得分
	showScore(v);



	return 0;
}

002 stack容器 先进后出

/*
总结
入栈--push
出栈--pop
返回栈顶--top
判断是否为空--empty
返回栈大小--size
*/



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

void test01()
{
	stack<int>s;

	//入栈
	s.push(10);
	s.push(20);
	s.push(30);
	s.push(40);


	//只要栈不为空
	while (!s.empty())
	{
		//查看栈顶元素
		cout << "栈顶元素为: " << s.top() << endl;

		//出栈
		s.pop();

	}
	cout << "栈的大小为: " << s.size() << endl;



}

int main()
{
	test01();



	return 0;

}

002 queue 容器先进先出

/*
总结:
入队---push
出队---pop
返回队头元素---front
返回队尾元素---back
判断队列是否为空---empty
返回队列大小---size


*/








#include<iostream>
using namespace std;
#include<queue>
#include<string>
class Person
{
public:

	Person(string name, int score)
	{
		this->m_Name = name;
		this->m_Score = score;

	}

	string m_Name;
	int m_Score;

};

void test01()
{

	//创建队列
	queue <Person>q;

	//准备数据
	Person p1("老铁",21);
	Person p2("老tou", 41);
	Person p3("老rt", 671);
	Person p4("老st", 41);

	//入列
	q.push(p1);
	q.push(p2);
	q.push(p3);
	q.push(p4);

	//判断队列是否为空,查看队头 队尾 出队


	while (!q.empty())
	{
		//查看队头

		cout << "队头元素  --姓名: " << q.front().m_Name << "年龄:" << q.front().m_Score << endl;


		//查看队尾
		cout << "队尾元素  --姓名: " << q.back().m_Name << "年龄:" << q.back().m_Score << endl;

		//出队
		q.pop();


	}
	cout << "队列的大小:" << q.size()<< endl;


}
int main()
{
	test01();



	return 0;

}

002 list 容器_构造函数

#include<iostream>
#include<list>
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容器
	list<int>L1;
	//添加数据
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(40);
	L1.push_back(50);
	L1.push_back(60);

	printList(L1);

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

	printList(L2);

	//拷贝构造


	list<int>L3(L2);
	printList(L3);

	list<int>L4(10, 200);
	printList(L4);






}

int main()
{
	test01();


	return 0;
}
/*
总结:
反转:--reverse
排序:---sort(成员函数

*/


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

1:list 容器不可以用[] 访问容器中的元素
2:也不可以用at 的方式访问
原因时list本质是链表,不是用连续性空间存储数据,也不支持随机访问 it+1

*/

void printList(list<int>& L)
{
	for (list<int>::iterator it = L.begin(); it != L.end(); it++)
	{
		cout << *it << " ";

	}
	cout << endl;
}

void test01()

{
	list<int>L1;

	L1.push_back(23);
	L1.push_back(43);
	L1.push_back(26);
	L1.push_back(7);

	cout << "反转前: " << endl;
	printList(L1);



}

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

}

void test02()

{
	list<int>L1;

	L1.push_back(23);
	L1.push_back(43);
	L1.push_back(26);
	L1.push_back(7);

	//反转
	cout << "反转后" << endl;
	L1.reverse();
	printList(L1);

	//所有不支持随机访问迭代器的容器,不可以用标准算法
	//不支持随机访问迭代器的容器,内部会提供对应的一些算法
	//sort (L1.begin(),L1,end());

	L1.sort();//默认排序规则 从小到大 升序

	cout << "排序后: " << endl;
	printList(L1);


	L1.sort(myCompare);
	printList(L1);

}

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




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值