c++学习笔记10

STL

STL初识

STL的诞生是为了建立数据结构和算法的一套标准

STL概念

STL 称为 标准模板库
STL从广义上分为 : 容器、算法、迭代器
容器和算法之间通过迭代器进行无缝连接
STL几乎所有的代码都采用了模板类或者模板函数

STL六大组件

容器、算法、迭代器、仿函数、适配器、空间配置器

1.容器:各种数据结构,如vector、list、deque、set、map等、用来存放数据
2.算法:各种常用的算法,如sort、find、copy、for_each等
3.迭代器:扮演容器和算法之间的胶合剂
4.仿函数:行为类似函数可作为算法的某种策略
5.适配器:一种用来修饰容器或者仿函数或迭代器接口的东西
6.空间配置器:负责空间的配置与管理
容器:
序列式容器:强调值的排序,其中每个元素都有固定的位置
关联式容器:二叉树结构,各元素之间没有严格的物理上的顺序关系
算法:
质变算法:是指运算过程中会更改区间内的元素的内容。如拷贝构造、替换、删除等
非质变算法:是指运算过程中不会更改区间内的元素内容,如查找、计数、遍历等

迭代器种类:
在这里插入图片描述

vector

vector 数据结构和数组非常相似,也称为单端数组
不同之处在于数组是静态空间,而vector可以动态扩展

动态扩展:

并不是在原有空间之后续接新空间,而是找更大的内存空间,然后将原数组拷贝到新空间,释放原空间

图示:

在这里插入图片描述

vector容器的构造
#include<iostream>
using namespace std;
#include<vector>  


void printVector(vector<int> &v1) {
	for (vector<int >::iterator it = v1.begin(); it != v1.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}
//vector容器的构造
void test01() {
	//1.无参构造
	vector<int> v1;
	for (int i = 0; i < 10; i++) {
		v1.push_back(i);
	}
	printVector(v1);

	//2.通过区间方式进行构造 将迭代器传入
	vector<int> v2(v1.begin(), v1.end());
	printVector(v2);

	//3.n个elem方式构造 10个100来初始化	
	vector<int> v3(10, 100);
	printVector(v3);

	//4.拷贝构造
	vector<int> v4(v3);
	printVector(v4);

}


int main() {
	test01();
	system("pause");
	return 0;
}
vector赋值操作
#include<iostream>
using namespace std;
#include<vector>

void printVector(vector<int> &v) {
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {

		cout << *it << " ";
	}
	cout << endl;
}
//vector的赋值
void test01() {
	vector<int> v1;
	for (int i = 0; i < 10; i++) {
		v1.push_back(i);
	}
	printVector(v1);

	//赋值 operator=
	vector<int> v2;
	v2 = v1;
	printVector(v2);

	//assign
	vector<int> v3;
	v3.assign(v1.begin(), v1.end());
	printVector(v3);

	//n个elem 方式赋值
	vector<int> v4;
	v4.assign(10, 100);
	printVector(v4);
}
int main() {
	test01();
	system("pause");
	return 0;
}
vector容量和大小
#include<iostream>
using namespace std;
#include<vector>

void printVector(vector<int> &v) {

	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it <<" ";
	}
	cout << endl;
}
//vector容器的容量和大小操作
void test01() {
	vector<int> v1;
	for (int i = 0; i < 10; i++) {
		v1.push_back(i);
	}
	printVector(v1);

	if (v1.empty()) {//为真 代表容器为空
		
		cout << "v1为空" << endl;
	}
	else {
		cout << "v1不为空" << endl;
		cout << "v1的容量为:" << v1.capacity() << endl;
		cout << "v1的大小为:" << v1.size() << endl;
	}

	//重新指定大小
	//如果重新指定的比原来的长了,默认用0来填充新的位置
	//v1.resize(15);
	//利用重载版本,可以指定默认填充值,参数2
	v1.resize(15, 100);
	printVector(v1);
	cout << "v1的容量为:" << v1.capacity() << endl;
	cout << "v1的大小为:" << v1.size() << endl;
	

	//如果重新指定比原来短了,超出的部分会删除掉
	v1.resize(5);
	printVector(v1);
	cout << "v1的容量为:" << v1.capacity() << endl;
	cout << "v1的大小为:" << v1.size() << endl;
}
int main() {
	test01();
	system("pause");
	return 0;
}
vector插入和删除
#include<iostream>
using namespace std;
#include<vector>

void printVector(vector<int> &v) {
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {

		cout << *it << " ";
	}
	cout << endl;

}
//插入和删除
void test01() {
	vector<int> v1;
	//尾插法
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(30);
	v1.push_back(40);
	v1.push_back(50);

	//遍历
	printVector(v1);

	//尾删
	v1.pop_back();//删除最后一个
	printVector(v1);

	//插入  第一个参数是迭代器
	v1.insert(v1.begin(), 100);//在头部插入100
	v1.insert(v1.end(), 200);//在尾部插入200
	printVector(v1);
	v1.insert(v1.begin(), 2, 1000);//在头部插入2个1000
	printVector(v1);

	//删除 参数是迭代器
	v1.erase(v1.begin());//删除头部一个元素
	v1.erase(v1.end()-1);//删除一个头部 和 一个尾部
	printVector(v1);

	//清空
	//v1.erase(v1.begin(), v1.end());
	v1.clear();
	printVector(v1);
}
int main() {
	test01();
	system("pause");
	return 0;
}
vector数据存取
#include<iostream>
using namespace std;
#include<vector>
//vector容器中的数据存取
void test01() {
	vector<int> v1;
	for (int i = 0; i < 10; i++) {
		v1.push_back(i);
	}

	//利用[]的方式来访问vector中的元素
	for (int i = 0; i < v1.size(); i++) {
		cout << v1[i] << " ";
	}
	cout << endl;

	//利用at方式来访问元素	
	for (int i = 0; i < v1.size(); i++) {
		cout << v1.at(i) << " ";
	}
	cout << endl;

	//获取第一个元素
	cout << "第一个元素为:" << v1.front() << endl;

	//获取最后一个元素
	cout << "最后一个元素为:" << v1.back() << endl;	
}
int main() {
	test01();
	system("pause");
	return 0;
}
vector互换容器
图示:

在这里插入图片描述

代码:
#include<iostream>
using namespace std;
#include<vector>
//vector容器的互换
void printVector(vector<int> &v) {
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
		
	}
	cout << endl;
}
//1.基本使用
void test01() {

	vector<int> v1;
	for(int i=0;i<10;i++){

		v1.push_back(i);
	}
	cout << "----------------交换前------------------------" << endl;
	printVector(v1);

	vector<int> v2;
	for (int i = 10; i > 0; i--) {
		v2.push_back(i);
	}
	printVector(v2);

	cout << "----------------交换后------------------------" << endl;
	v1.swap(v2);
	printVector(v1);
	printVector(v2);
}
//2.实际用途
//巧用swap可以收缩内存空间
void test02() {

	vector<int> v;
	for (int i = 0; i < 100000; i++) {
		v.push_back(i);
	}
	cout << "v的容量为:" << v.capacity() << endl;
	cout << "v的大小为:" << v.size() << endl;

	v.resize(3);//重新指定大小
	cout << "v的容量为:" << v.capacity() << endl;
	cout << "v的大小为:" << v.size() << endl;
	//巧用swap收缩内存
	vector<int>(v).swap(v);
	/***********
	vector<int>(v)//匿名对象
	调用拷贝构造
	来根据传入的v创建出一个新的对象
	但是这个对象没有名字即匿名
	这个匿名对象会根据v中实际的元素个数来创建对象(容量为3 大小为3)
	.swap(v);  容器交换
	让v指向新创建出来的对象
	让匿名对象指向原来的大空间
	因为匿名对象在这行代码执行完后会被系统释放
	所以达到收缩内存的效果
	***********/

	cout << "v的容量为:" << v.capacity() << endl;
	cout << "v的大小为:" << v.size() << endl;

}
int main() {
	//test01();
	test02();
	system("pause");
	return 0;
}
vector预留空间
#include<iostream>
using namespace std;
#include<vector>
//vector预留空间
void test01() {
	vector<int> v;

	//利用reserve预留空间
	v.reserve(100000);//预留出100000的空间 就不用开辟30次来存放这100000个数据 只需要开辟一次就行
	
	int num = 0;//统计开辟的次数
	int* p = NULL;
	for (int i = 0; i < 100000; i++) {
		v.push_back(i);

		if (p != &v[0]) {//计算开辟了多少次空间来存放100000个数据
			p = &v[0];
			num++;
		}
	}
	cout << "num= " << num << endl; 

}
int main() {
	test01();
	system("pause");
	return 0;
}
vector存放内置的数据类型
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>//标准算法头文件 algorithm
//vector容器存放内置数据类型

void myPrint(int val) {
	cout << val << endl;
}
void test01() {
	//创建了一个vector容器
	vector<int> v;
	//向容器中插入数据
	v.push_back(10);
	v.push_back(20);
	v.push_back(30);
	v.push_back(40);
	v.push_back(50);

	通过迭代器访问容器中的数据
	//vector<int>::iterator itBegin = v.begin();//起始迭代器 指向容器中第一个元素
	//vector<int>::iterator itEnd = v.end();//结束迭代器  指向容器中最后一个元素的下一个位置

	第一种遍历方式
	//while (itBegin != itEnd) {
	//	cout << *itBegin << endl;
	//	itBegin++;
	//}


	第二种遍历方式
	//for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
	//	cout << *it << endl;
	//}


	//第三种遍历方式 利用STL中提供的遍历算法
	for_each(v.begin(), v.end(), myPrint);

	
}

int main() {
	test01();
	system("pause");
	return 0;
}
vector存放自定义数据类型
#include<iostream>
using namespace std;
#include<vector>
#include<string>

//vector容器中存放自定义数据类型 类
class Person {
public:
	Person(string name,int age) {
		this->m_Name = name;
		this->m_Age = age;
	}

	string m_Name;
	int m_Age;
};

void test01() {

	vector<Person> v;


	Person p1("aaa", 10);
	Person p2("bbb", 20);
	Person p3("ccc", 30);
	Person p4("ddd", 40);
	Person p5("eee", 50);

	//向容器中添加数据
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);

	//遍历容器中的数据
	for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {
		cout << it->m_Name<<"  " << it->m_Age<< endl;
	}
}

//存放自定义的数据类型 指针
void test02() {
	vector<Person *> v;


	Person p1("aaa", 10);
	Person p2("bbb", 20);
	Person p3("ccc", 30);
	Person p4("ddd", 40);
	Person p5("eee", 50);

	//向容器中添加数据
	v.push_back(&p1);
	v.push_back(&p2); 
	v.push_back(&p3);
	v.push_back(&p4);
	v.push_back(&p5);

	for (vector<Person*>::iterator it = v.begin(); it != v.end(); it++) {
		cout << (*(*it)).m_Name << "  " << (*(*it)).m_Age << endl;
		cout << (*it)->m_Name << "  " << (*it)->m_Age << endl;
	}
}
int main() {
	//test01();
	test02();
	system("pause");
	return 0;
}
vector容器嵌套容器
#include<iostream>
using namespace std;
#include<vector>
//容器嵌套容器
void test01() {
	vector<vector<int>> v; 

	//创建小容器
	vector<int> v1;
	vector<int> v2;
	vector<int> v3;
	vector<int> v4;


	//向小容器中添加数据
	for (int i = 0; i < 4; i++) {
		v1.push_back(i + 1);
		v2.push_back(i + 2);
		v3.push_back(i + 3);
		v4.push_back(i + 4);
	}

	

	//将小容器插入到大的容器中
	v.push_back(v1);
	v.push_back(v2);
	v.push_back(v3);
	v.push_back(v4);



	//通过大容器把所有的数遍历一遍
	for (vector<vector<int>>::iterator it1 = v.begin(); it1 != v.end(); it1++) {
		for (vector<int>::iterator it2 = (*it1).begin(); it2 != (*it1).end(); it2++) {
			cout << *it2<<"\t" ;
			
		}
		cout << endl;


	}


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

string

string 构造函数
#include<iostream>
using namespace std;

//string 的构造函数
void test01() {

	string s1;//无参构造
	s1 = "Hello World";
	cout << "s1= " << s1 << endl;

	const char* str = "Hello World";
	string s2(str);//有参构造

	cout << "s2= " << s2 << endl;

	string s3(s2);//拷贝构造
	cout << "s3= " << s3 << endl;

	string s4(10, 'a');//重载
	cout << "s4= " << s4 << endl;
}
int main() {
	test01();
	system("pause");
	return 0;
}
string赋值操作
#include<iostream>
using namespace std;

//string 的赋值操作
void test01() {
	string str1;
	str1 = "Hello World!";
	cout << "str1= " << str1 << endl;

	string str2;
	str2 = str1;
	cout << "str2= " << str2 << endl;
	
	string str3;
	str3 = 'a';
	cout << "str3= " << str3 << endl;

	string str4;
	str4.assign("Hello C++");
	cout << "str4= " << str4 << endl;

	string str5;
	str5.assign("Hello C++", 5);
	cout << "str5= " << str5 << endl;

	string str6;
	str6.assign(str5);
	cout << "str6= " << str6 << endl;

	string str7;
	str7.assign(10, 'c');
	cout << "str7= " << str7 << endl;

}
int main() {
	test01();
	system("pause");
	return 0;
}
string字符串拼接
#include<iostream>
using namespace std;

//string 字符串拼接

void test01() {

	string str1 = "我";
	str1 += "爱";
	cout << "str1= " << str1 << endl;
	
	str1 += ':';
	cout << "str1= " << str1 << endl;

	string str2 = "小虎贝";
	//str1 += str2;
	//str1 = str1 + str2;
	cout << "str1= " << str1 << endl;


	string str3 = "I";
	str3.append("Love");
	cout << "str3= " << str3 << endl; 

	str3.append("game abcde", 4);
	cout << "str3= " << str3 << endl;

	str3.append(str2);
	cout << "str3= " << str3 << endl;

	str3.append(str2, 0, 4);
	cout << "str3= " << str3 << endl;




}
int main() {
	test01();
	system("pause");
	return 0;
}
string 查找和替换
#include<iostream>
using namespace std;

//字符串查找和替换

//1.查找
void test01() {
	//find
	string str1 = "abcdefgde";
	int pos = str1.find("def");
	if (pos == -1) {
		cout << "未找到指定字符串" << endl;
	}
	else {
		cout << "pos= " << pos << endl;
	}
	


	//rfind 和 find 的区别
	//rfind从右往左查找 find 从左往右查找
	pos=str1.rfind("de");
	cout << "pos= " << pos << endl;

}
//2.替换
void test02() {
	string str1 = "abcdefg";
	//从1号位置起 3个字符 替换为"1111"
	str1.replace(1, 3, "1111");
	cout << "str1=" << str1 << endl;
	
}
int main() {
	//test01();
	test02();
	system("pause");
	return 0;
}
string字符串比较

字符串比较是按字符的ASCII码进行对比
等于 返回 0
大于 返回 1
小于 返回 -1

#include<iostream>
using namespace std;

//字符串比较
void test01() {
	string str1 = "hello";
	string str2 = "xello";

	if (str1.compare(str2) == 0) {
		cout << "str1等于str2" << endl;
	}
	else if (str1.compare(str2) > 0) {
		cout << "str1大于str2" << endl;
	}
	else {
		cout << "str1小于str2" << endl;
	}

}

int main() {
	test01();
	system("pause");
	return 0;
}
string字符串存取
#include<iostream>
using namespace std;

//string 字符串的存取
void test01() {
	string str = "hello world";
	cout << "str= " << str << endl;

	//读

	//1.通过 [] 访问单个字符
	for (int i = 0; i < str.size(); i++) {
		cout << str[i] << " ";
	}
	cout << endl;


	//2.通过at方式访问单个字符
	for (int i = 0; i < str.size(); i++) {

		cout << str.at(i) << " ";
	}
	cout << endl;


	//写

	//修改单个字符
	str[0] = 'x';
	cout << "str= " << str << endl;

	str.at(1) = 'x';
	cout << "str= " << str << endl;



}

int main() {
	test01();
	system("pause");
	return 0;
}
string中的插入和删除
#include<iostream>
using namespace std;

//字符串的插入和删除
void test01() {
	string str = "Hello World!";

	//插入 从一号位置开始插入"111"
	str.insert(1, "111");
	cout << "str= " << str << endl;

	//删除 从第1个位置起删除3个
	str.erase(1, 3);
	cout << "str= " << str << endl;
}

int main() {
	test01();
	system("pause");
	return 0;
}
string中的子串
#include<iostream>
using namespace std;

//string 求子串
void test01() {
	string str = "HelloWorld!";
	string subStr = str.substr(1,3);
	cout << "subStr= " << subStr << endl;
}

//实用操作	
void test02() {

	string email = "zhangsan@sina.com";

	//从邮件地址中获取用户名信息
	int pos = email.find("@");//8

	string userName = email.substr(0, pos);
	cout << "userName= " << userName << endl;
}
int main() {
	//test01();
	test02();
	system("pause");
	return 0;
}

deque

功能:

双端数组,可以对头端进行插入删除操作

deque 与 vector区别:

vector对于头部的插入删除效率低,数据量越大,效率越低
deque相对而言,对头部的插入删除速度会比vector快
vector访问元素时的速度会比deque快,这和两者内部实现有关

图示:

在这里插入图片描述
中控器维护的是每个缓冲区的地址,使得使用deque时像一片连续的内存空间
在这里插入图片描述

deque容器的构造函数
#include<iostream>
using namespace std;
#include<deque>

void printDeque(const deque<int> &d) {
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {//const_iterator 为只读迭代器
		//*it =100; 加const 容器里的数据现在是不可以修改
		cout << *it << " ";
	}
	cout << endl;
}
//deque构造函数
void test01() {
	deque<int> d1;
	for (int i = 0; i < 10; i++) {
		d1.push_back(i);

	}
	printDeque(d1);


	deque<int> d2(d1.begin(), d1.end());
	printDeque(d2);


	deque<int> d3(10, 100);
	printDeque(d3);


	deque<int> d4(d3);
	printDeque(d4);
}
int main() {
	test01();
	system("pause");
	return 0;
}
deque复制操作
#include<iostream>
using namespace std;
#include<deque>

void printDeque(const deque<int> &d) {
	for (deque<int>::const_iterator cit = d.begin(); cit != d.end(); cit++) {

		cout << *cit << " " ;
	}
	cout << endl;
	
}
//deque容器赋值操作
void test01() {

	deque<int> d1;
	for (int i = 0; i < 10; i++) {
		d1.push_back(i);

	}
	printDeque(d1);

	//operator=赋值
	deque<int> d2;
	d2 = d1;
	printDeque(d2);

	//assign 赋值
	deque<int> d3;
	d3.assign(d1.begin(), d1.end());
	printDeque(d3);

	//n个
	deque<int> d4;
	d4.assign(10, 100);
	printDeque(d4);


}
int main() {
	test01();
	system("pause");
	return 0;
}
deque大小操作
#include<iostream>
using namespace std;
#include<deque>
//deque容器的大小操作
void printDeque(const deque<int>& d) {
	for (deque<int>::const_iterator cit = d.begin(); cit != d.end(); cit++) {
		cout << *cit << " ";

	}
	cout << endl;
}

void test01() {
	deque<int> d1;
	for (int i = 0; i < 10; i++) {
		d1.push_back(i);
	}
	printDeque(d1);

	if (d1.empty()) {
		cout << "d1为空" << endl;
	}
	else {
		cout << "d1不为空" << endl;
		cout << "d1的大小为:" << d1.size() << endl;
		//deque容器没有容量概念 可以无限增加
	}

	//重新指定大小
	//d1.resize(15);
	d1.resize(15, 1);//超出的用1来填充
	printDeque(d1);


	d1.resize(5);
	printDeque(d1);

}
int main() {
	test01();
	system("pause");
	return 0;
}
deque插入和删除
#include<iostream>
using namespace std;
#include<deque>
//deque容器的插入和删除

void printDeque(const deque<int>& d) {

	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}
//两端操作
void test01() {
	deque<int> d1;
	
	//尾插
	d1.push_back(10);
	d1.push_back(20);

	//头插
	d1.push_front(100);
	d1.push_front(200);

	//200 100 10 20
	printDeque(d1);

	//尾删
	d1.pop_back();
	//200 100 10
	printDeque(d1);

	//头删
	d1.pop_front();
	// 100 10
	printDeque(d1);
}

void test02() {

	deque<int> d1;
	d1.push_back(10);
	d1.push_back(20);
	d1.push_front(100);
	d1.push_front(200);

	//200 100 10 20
	printDeque(d1);

	//insert插入
	d1.insert(d1.begin(), 1000);
	printDeque(d1);

	d1.insert(d1.begin(), 2, 10000);
	printDeque(d1);

	//按照区间插入	
	deque<int> d2;
	d2.push_back(1);
	d2.push_back(2);
	d2.push_back(3);

	d1.insert(d1.begin(), d2.begin(), d2.end());
	printDeque(d1);

}

void test03() {
	deque<int> d1;
	d1.push_back(10);
	d1.push_back(20);
	d1.push_front(100);
	d1.push_front(200);

	//删除
	d1.erase(d1.begin());
	printDeque(d1);

	//按区间删除
	//d1.erase(d1.begin(), d1.end());
	//清空
	d1.clear();
	printDeque(d1);
}
int main() {
	//test01();
	//test02();
	test03();
	system("pause");
	return 0;
}
deque数据存取
#include<iostream>
using namespace std;
#include<deque>
//deque容器数据存取
void test01() {

	deque<int> d;
	d.push_back(10);
	d.push_back(20);
	d.push_back(30);
	d.push_front(100);
	d.push_front(200);
	d.push_front(300); 

	//通过[]方式访问元素
	for (int i = 0; i < d.size(); i++) {
		cout << d[i] << " ";
	}
	cout << endl;

	//通过at的方式访问元素
	for (int i = 0; i < d.size(); i++) {
		cout << d.at(i) << " ";
	}
	cout << endl;

	cout << "第一个元素为:" << d.front() << endl;
	cout << "最后一个元素为:" << d.back() << endl;
}
int main() {

	test01();
	system("pause");
	return 0;
}
deque排序
#include<iostream>
using namespace std;
#include<deque>
#include<algorithm>
void printDeque(const deque<int> &d) {
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
		cout << *it << " ";

	}
	cout << endl;
}
//deque容器排序
void test01() {
	deque<int> d;
	d.push_back(10);
	d.push_back(20);
	d.push_back(30);
	d.push_front(100);
	d.push_front(200);
	d.push_front(300);
		
	printDeque(d);

	//排序  默认从小到大 升序
	//对于支持随机访问的迭代器的容器,都可以利用sort算法直接对其进行排序

	sort(d.begin(),d.end());
	printDeque(d);
}
int main() {

	test01(); 
	system("pause");
	return 0;
}
案例评委打分
#include<iostream>
using namespace std;
#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);

		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);
		}

		//排序
		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));
	//1.创建5名选手
	vector<Person> v;//存放选手的容器
	createPerson(v);
	//测试
	//for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {
	//	cout << it->m_Name << " " << it->m_Score << endl;
	//}

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


	//3.显示最后得分
	showScore(v);
	system("pause");
	return 0;
}

stack容器

图示:

在这里插入图片描述

代码:
#include<iostream>
using namespace std;
#include<stack>

//栈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();
	system("pause");
	return 0;
}

queue容器

图示:

在这里插入图片描述

代码:
#include<iostream>
using namespace std;
#include<queue>
//队列 queue
class Person {
public:
	string m_Name;
	int m_Age;

	Person(string name,int age) {
		this->m_Name = name;
		this->m_Age = age;
	}
};
void test01() {
	//创建队列
	queue<Person> q;

	//准备数据
	Person p1("张三", 18);
	Person p2("李四", 12);
	Person p3("王五", 23);
	Person p4("赵六", 15);

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

	//判断只要队列不为空,查看队头,查看队尾,出队
	while (!q.empty()) {
		//查看对头
		cout << "队头--------" << q.front().m_Name << "  年龄:" << q.front().m_Age << endl;

		//查看队尾
		cout << "队尾--------" << q.back().m_Name << "  年龄:" << q.back().m_Age << endl;

		//出队
		q.pop();
	}
	cout << "队列的大小为:" << q.size() << endl;
}
int main() {
	test01();
	system("pause");
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值