1 容器的学习

容器的学习

1 Vector容器(序列)

1.1 Vector 的构造函数

v.capacity();  //容器容量
v.size();      //容器大小
v.at(int idx); //用法和[]运算符相同
v.push_back(); //尾部插入
v.pop_back();  //尾部删除
v.front();     //获取头部元素
v.back();      //获取尾部元素
v.begin();     //头元素的迭代器
v.end();       //尾部元素的迭代器
v.insert(pos,elem); //pos是vector的插入元素的位置
v.insert(pos, n, elem) //在位置pos上插入n个元素elem
v.insert(pos, begin, end);
v.erase(pos);   //移除pos位置上的元素,返回下一个数据的位置
v.erase(begin, end); //移除[begin, end)区间的数据,返回下一个元素的位置
reverse(pos1, pos2); //将vector中的pos1~pos2的元素逆序存储

(1)默认构造

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

(2)区间构造

vectorv2(v1.begin(),v1.end());

(3)n个element构造

vectorv3(10,100); //10个100

(4)拷贝构造

vectorv4(v3);

1.2 vector 的赋值操作

(1) 利用“ = ”进行赋值

vector<int>v1;
vector<int>v2;
v2 = v1;

(2)利用 assign 赋值

vector<int>v3;
v3.assign(v1.begin(), v1.end());//从v1的begin到v1的end
print(v3);
vector<int>v4;
v4.assign(10, 100);

1.3 vector 的容量和大小

(1)判断容器是否为空

if (v1.empty())
{
	cout << "vector v1 is empty" << endl;
}
else
{
	cout << "vector v1 is not empty and the capacitty of v1 is " << v1.capacity() << endl;
	cout << "the size of v1 is " << v1.size() << endl;
}

(2)重新指定容器的大小

v1.resize(15);	//超出的部分使用 0 来填充
v1.resize(15,1);	//用 1 来代替自动填充的 0
v1.resize(5);

1.4 vector 容器的插入和删除

(1)插入——insert

vector<int>v1;
v1.push_back(10);v1.push_back(20);v1.push_back(30);	//尾插
v1.push_back(40);v1.push_back(50);print(v1);
v1.pop_back();print(v1);	//头插
v1.insert(v1.begin(), 100);print(v1);
v1.insert(v1.begin(),2, 200);print(v1);	//从指定位置插入指定长度的指定的数据

(2)删除——erase

v1.erase(v1.begin());print(v1);
v1.erase(v1.begin(), v1.end()-1); print(v1);//从开始删除直到end - 1
v1.clear(); print(v1);	//删除全部

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RsnSvsf0-1624936226161)(C:\Users\86135\AppData\Roaming\Typora\typora-user-images\1616896748909.png)]

1.5 vector 容器的数据存取

(1)利用" [] " 访问容器内元素

for (int i = 0; i < v1.size(); i++) // size 判断容器内元素的大小
{
	cout << v1[i] << " ";	//遍历容器内的元素
}

(2)利用 at 访问容器内元素

for (int i = 0; i < v1.size(); i++)
{
	cout << v1.at(i) << " ";
})

(3)第一个和最后一个

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

1.6 vector 容器元素的互换

(1)元素的互换 swap

首先设置好两个vector容器 v1和v2 并插入不同的数据

v1.swap(v2);

(2)vector 的容量

 vector<int>v;
	 for (int i = 0; i < 100000; i++)
	 {
		 v.push_back(i);
	 }
	 cout << "the capacity of vector v is " << v.capacity() << endl;
	 cout << "the size of vector of vector v is " << v.size() << endl;
	 v.resize(3);		//不会改变容器的capacity	
	 cout << "the capacity of vector v is " << v.capacity() << endl;
	 cout << "the size of vector of vector v is " << v.size() << endl;
	 vector<int>(v).swap(v);	//将容器的capacity 和 size 都转换为size
	 cout << "the capacity of vector v is " << v.capacity() << endl;
	 cout << "the size of vector of vector v is " << v.size() << endl;

1.7 vector 容器的预留空间

减少因为要插入的数据过大是动态扩展的次数(因为如果数据过大时,在插入时不是一次性插入完的,而是依次开辟空间而动态扩展的)

vector<int>v1;
int num = 0;
int *p = NULL;
//v1.reserve(100000);
for (int i = 0; i < 100000; i++)
{
	v1.push_back(i);
	if (p != &v1[0])
	{
		p = &v1[0];
		num++;
	}
}
cout << num << endl;

2 string 容器(序列)

2.1 string 容器的构造函数

(1)默认构造

string s1;//默认构造
const char *str = "hello world";
s1 = str;

(2)有参构造和拷贝构造

string s2(str);//有参构造函数
cout << "s2 =  " << s2 << endl;
string s3(s2);	//拷贝构造函数
cout << "s3 =  " << s3 << endl;

(3)赋值多个字符 char

string s4(10, 'a');
cout << "s4 =  " << s4 << endl;

2.2 string 的赋值操作

(1)重载“ = ” 运算符

string str1;
str1 = "hello world";
cout << "str1 =  " << str1 << endl;

(2)利用 assign 赋值

string str2;
str2.assign("hello cpp");
cout << "str2 =  " << str2 << endl;

string str3;
str3.assign(str1, 2);		//这里注意一下   str1  和 "hello world"的区别
cout << "str3 =  " << str3 << endl;

string str4;
str4.assign(10, 'a');
cout << "str4 =  " << str4 << endl;

2.3 string 的拼接操作

(1)重载“ += ”运算符

这里需要注意一点的是:

string 和 char 不能直接使用 ” + “ 而是应该使用 ” += “,但是两个string 可以直接使用

string s1("我");
s1 += "爱玩游戏";
s1 += '!';     //英文字符
cout << "s1 =  " << s1 << endl;

(2)append 拼接

string s2("dongehning");
string s3("I ");
s3.append("love ");
s3.append("China win",5);
cout << "s3 =  " << s3 << endl;

s3.append(s2);
cout << "s3 =  " << s3 << endl;

string s4;
s4.append(s1, 2, 4);  //从第2号截取4个字符
cout << "s4 =  " << s4 << endl;

2.4 string 的查找和替换

(1)查找 find和rfind

find是从左往右查找,rfind是从右往左查找

string str1("abcdefgde");
int pos = str1.find("de");
if (pos == string::npos)		//npos == -1
{
	cout << "未找到该字符串" << endl;
}
else
cout << "pos =  " << pos << endl;

//find是从左往右查找,rfind是从右往左查找
pos = str1.rfind("de");
cout << "pos =  " << pos << endl;

(2)替换 replace

string str1 = "abcdefg";
str1.replace(1, 3, "1111"); //把从第1号元素开始的位置后面的三个元素替换为 所给出的字符串
cout << "str1 =  " << str1 << endl;

(3)string 的其他知识

此外string还有很多其他的接口 比如说:

c_str();返回const char*

substr;返回一个字串

还有 copy find_last_of 等等的接口需要以后继续学习

2.5 string 的比较和存取

(1)比较——compare

compare 返回一个int值 相等则为 0 > 1 < -1

string str1("hellp");
string str2("hello");
if (str1.compare(str2) == 0)
{
	cout << "str1 = str2" << endl;
}
else if (str1.compare(str2) > 0)
{
	cout << "str1 > str2" << endl;
}
else
{
	cout << "str1 < str2" << endl;
}

(2)利用重载符” [] “获取

string str1("hello");
cout << "str =  " << str1 << endl;
for (int i = 0; i < str1.size(); i++)
{
	cout << str1[i] << " ";
}
cout << endl;

(3)利用 at 获取

for (int i = 0; i < str1.size(); i++)
{
	cout << str1.at(i) << " ";
}
cout << endl;

(4)利用[]和at往里面写值

//写
str1[0] = 'x';
str1.at(1) = 'o';
cout << "str1 =  " << str1 << endl;

2.6 string 的插入和删除

(1)插入——insert

string str("hello");
str.insert(1, "111");

(2)删除——erase

int pos = str.find("111");
str.erase(pos, 3);	//从位置pos开始,往下删除三个字符
cout << "str = " << str << endl;

2.7 string 的子串 substr

(1)substr的两种重载函数

string str("abcdefg");
string sstr = str.substr(1,3);//从第一个往后数三个
string ssstr = str.substr(1);//从第一个直到最后
cout << "sstr =  " << sstr << endl;
cout << "ssstr =  " << ssstr << endl;

(2)和find结合使用

string str("dhn13503224345@163.com");
int pos = str.find('@');
string sstr = str.substr(0, pos);
cout << "sstr =  " << sstr << endl;

3 list容器(序列)

3.1 list 容器的构造函数

和前面的string 和vector 相似 这里一并给出

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);	//拷贝构造
list<int>l4(10, 1000); 
print(l4);	//构造具体个数

3.2 list 容器的赋值和交换

(1)list赋值——重载“ = ”

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

(2)assign赋值

list<int>l3; 
l3.assign(l2.begin(), l2.end()); 	//设定指定的区间进行赋值
print(l3);
list<int>l4; 
l4.assign(10, 100); 
print(l4);

(3)list交换——swap

cout << "交换后: " << endl;
l1.swap(l4); 	//将l1和l4进行交换
print(l1); 
print(l4);

3.3 list 容器的大小操作

list大小的操作和前面的类似 无非size empty 和 resize 但是没有容量capacity

因此这里一并给出

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;
}
else
{
	cout << "l1不为空" << endl;
	cout << "l1得元素个数为: " << l1.size() << endl;
}
l1.resize(10/*,100*/); 
print(l1);//缺省值为0  后面得参数可以进行指定 但是前面得数据是不会改变得
l1.resize(2); 
print(l1);

3.4 list 容器的插入和删除

(1)list的插入

list<int>l1;
l1.push_back(10); l1.push_back(20); l1.push_back(30); 	//尾插
l1.push_front(100); l1.push_front(200); l1.push_front(300); print(l1);	//头插
l1.pop_back(); 		//删除最后一个元素
print(l1); 
l1.pop_front(); 	//删除第一个元素
print(l1);
l1.insert(/*++*/l1.begin(), 1000); print(l1);	//在指定位置插入指定的元素

(2)list的删除

l1.erase(l1.begin()); print(l1);
l1.push_back(10000); l1.push_back(10000); l1.push_back(10000); l1.push_back(10000); print(l1);
l1.remove(10000); 	//清除容器内所有的10000
print(l1);
//l1.erase(l1.begin(), l1.end()); print(l1);
l1.clear(); 	//清空操作等价于上面那一行
print(l1);

3.5 list 容器的数据存取

(1)不支持随机访问

list<int>l1;
l1.push_back(10); l1.push_back(20); l1.push_back(30); l1.push_back(40); 
print(l1);
cout << "l1 的第一个元素为: " << l1.front() << endl;
cout << "l1 的最后一个元素为: " << l1.back() << endl;
auto iter = l1.begin();		//auto = list<int>::itertor
/*iter += 1;*/		//错误,因为这里是不支持随机访问的
iter++; 
iter--;//双向操作  不支持随机访问

3.6 list容器的反转和排序

(1)list容器的反转

list<int>l1;
l1.push_back(10); l1.push_back(40); l1.push_back(30); l1.push_back(20); 
print(l1);
l1.reverse(); 		//将数组里面的元素进行反转
print(l1);

(2)list容器的排序

bool MyCompare(int a, int b)//下面排序要用到的回调函数
{
	//降序排列 则a > b
	return a > b;
}
//所有不支持随机访问迭代器的容器不可以用标准的算法  algorithm 因此 sort(l1.begin(),l1.end())  错误
l1.sort(); 
print(l1); 
/*l1.reverse(); print(l1);*/	//进行反转 可以实现和下面一行相同的功能
l1.sort(MyCompare); 	//相当于回调函数
print(l1);

4 map和multimap容器

4.1 map 容器的构造和赋值

map容器采用的是键对值,在进行构造和赋值的时候往往是成对加入的

(1)默认构造函数

map<int, string>m1;		//一个pair 键对值形式出现
m1.insert(pair<int, string>(1, "董贺宁"));
m1.insert(pair<int, string>(3, "yyw"));
m1.insert(pair<int, string>(2, "wy"));
m1.insert(pair<int, string>(4, "wbb"));
print(m1);

(2)拷贝构造

map<int, string>m2(m1);

print(m2);

(3)重载” = “进行赋值

map<int, string>m3; m3 = m2; 
print(m3);

4.2 map 容器的大小和交换

(1)一些常规的插入以及判断是否为空

map<int, int>m1;
m1.insert(pair<int, int>(1, 10)); 
m1.insert(make_pair(2, 20)); 
m1.insert(pair<int, int>(3, 30));
if (m1.empty())		//若容器为空则返回为ture
{
	cout << "the vector map is empty now!!!" << endl;
}
else
{
	cout << "the vector map is not empty now yet!!!" << endl;
}

(2)容器大小及交换

大小————size 交换————swap

cout << "the size of not empty vector map is " << m1.size() << endl;
map<int, int>m2;
m2.insert(pair<int, int>(4, 40)); m2.insert(pair<int, int>(5, 50)); 	m2.insert(pair<int, int>(6, 60));
m1.swap(m2); 
print2(m1); 
print2(m2);

4.3 map 容器的插入和删除

(1)四种插入方法

map<int, int>m1;
m1.insert(pair<int, int>(1, 10));
m1.insert(make_pair(2, 20));
m1.insert(map<int, int>::value_type(3, 30));
m1[4] = 40;		//不推荐使用这种插入方法  因为若把这行注释,运行下面那行则会自动插入一个0
cout << m1[4] << endl; 
print2(m1);

(2)删除方法

这里的删除方法和前面的容器一样,无非一些erase和clear 如下:

m1.erase(m1.begin()); 
print2(m1);
m1.erase(3); 
print2(m1);
m1.erase(m1.begin(), m1.end()); 
print2(m1);
//m1.clear(); print2(m1);

4.4 map 容器的查找和统计

(1)使用find进行查找

使用find进行查找时,注意查找的时key值,key值在map当中一定时唯一的只有一个

map<int, int>m1;
m1.insert(make_pair(1, 10)); m1.insert(make_pair(2, 20)); m1.insert(make_pair(3, 30));
auto iter = m1.find(3);		//查找的是key值
if (iter != m1.end())
{
	cout << "the elem has been exist in this map" << endl;
	cout << "the key is " << (*iter).first << " " << "the value is " << iter->second << endl;
}
else
{
	cout << "the elem has not been exist in this map" << endl;
}

(2)使用count计数

注意:count查找的同时是key值

int temp_int = m1.count(3);//map 的count值只能是0和1 multimap可以是>1的数
cout << "temp_int =  " << temp_int << endl;

4.5 map 容器的排序算法

class MyCompare_map
{
public:
	bool operator()(int v1, int v2)
	{
		return v1 > v2;		//这里选择的是降序的算法
	}
};
void print_map(map<int, int,MyCompare_map> &m)
{
	for (auto iter = m.begin(); iter != m.end(); iter++)
	{
		cout << iter->first << "  " << iter->second << endl;
	}
	cout << endl;
}
map<int, int,MyCompare_map>m1;	//在插入时就已经限定了插入的顺序,否则是一个默认的升序
m1.insert(make_pair(1, 10));
m1.insert(make_pair(2, 20));
m1.insert(make_pair(3, 30));
m1.insert(make_pair(4, 40));
m1.insert(make_pair(5, 50));
print_map(m1);		//打印函数

5 stack 栈的常用接口

使用栈(stack)的时候,需要注意栈的原则:先进后出

stack<int>s;
for (int i = 0; i < 5; i++)
{
	s.push(i);	//入栈
}
while (!s.empty())	//判断栈是否为空 为空则返回为true
{
	cout << s.top() << endl;	//栈顶
	s.pop();		//出栈
}
cout << s.size() << endl;	//统计栈的大小

6 队列queue

首先需要明确一点,队列和栈的出入是不同的,队列是先进先出的

class Person
{
public:
	Person(string name,int age)
	{
		this->m_name = name;
		this->m_age = age;
	}
	string m_name;
	int m_age;
};
queue<Person>q;
Person p1("张三", 18);Person p2("李四", 28);Person p3("王五", 38);Person p4("赵六", 48);
q.push(p1); q.push(p2); q.push(p3); q.push(p4);		//入队
while (!q.empty())
{
	cout << "队头为:" << endl << "姓名: " << q.front().m_name << "  年龄: " << q.front().m_age << endl;	//front为对头
	cout << "队尾为:" << endl << "姓名: " << q.back().m_name << "  年龄: " << q.back().m_age << endl;		//back为队尾
	q.pop();		//出队
}
cout << "现在队列的大小为:" << q.size() << endl;

7 deque容器(序列)

这里需要注意的是,deque的各种接口和前面的几乎相同这里直接粘贴

创建deque对象
(1) deque<int> d1;
(2) deque<int> d2(10);
基本操作:
(1) 元素访问:
d[i];
d.at[i];
d.front();
d.back();
d.begin();
d.end();
(2) 添加元素:
d.push_back();
d.push_front();
d.insert(pos,elem); //pos是vector的插入元素的位置
d.insert(pos, n, elem) //在位置pos上插入n个元素elem
d.insert(pos, begin, end);
(3) 删除元素:
d.pop_back();
d.pop_front();
d.erase(pos);   //移除pos位置上的元素,返回下一个数据的位置
d.erase(begin, end); //移除[begin, end)区间的数据,返回下一个元素的位置

7.1 deque 容器的构造函数

deque<int>d1;
for (int i = 0; i < 10; i++)
{
	d1.push_back(i);
}
print(d1);
deque<int>d2(d1.begin(), d1.end());print(d2);
deque<int>d3(10, 100); print(d3);
deque<int>d4(d3); print(d4);

7.2 deque 容器的赋值操作

deque<int>d1;
for (int i = 0; i < 10; i++)
{
	d1.push_back(i);
}
print(d1);
deque<int>d2; d2 = d1; print(d2);
deque<int>d3; d3.assign(d2.begin(), d2.end()); print(d3);
deque<int>d4; d4.assign(10, 100); print(d4);

7.3 deque 容器的大小操作

deque<int>d1;
for (int i = 0; i < 10; i++)
{
	d1.push_back(i);
}
print(d1);
if (d1.empty())
{
	cout << "deque d1 is empty now!!!!" << endl;
}
else
{
	//注意  没有deque容量的capitity
	cout << "deque d1 is not empry and the size of d1 is " << d1.size() << endl;
}
//d1.resize(15); print(d1);
d1.resize(15, 1); print(d1);
d1.resize(12); print(d1);

7.4 deque 容器的插入和删除

//尾插和头插
deque<int>d1;
for (int i = 10; i <= 20; i += 10)
{
	d1.push_back(i);
}
d1.push_front(100); d1.push_front(200); print(d1);
//尾删和头删
d1.pop_back(); d1.pop_front(); print(d1);
//指定位置进行删除
deque<int>d2;
d2.push_back(10); d2.push_back(20); d2.push_front(100); d2.push_front(200); print(d2);
d2.insert(d2.begin(), 1000); print(d2);
d2.insert(d2.begin(), 2, 10000); print(d2);
d2.insert(d2.begin(), d1.begin(), d1.end()); print(d2);
d2.erase(d2.begin()); print(d2);
//d2.erase(d2.begin(), d2.end()); print(d2);
d2.clear(); print(d2);

7.5 deque 容器的数据存取

deque<int>d1;
for (int i = 0; i < 10; i++)
{
	d1.push_back(i);
}
print(d1);
for (int i = 0; i < d1.size(); i++)
{
	cout << "第 " << i << " 个元素为: " << d1[i] << endl;
	cout << "第 " << i << " 个元素为: " << d1.at(i) << endl;
}
cout << "第 " << "1" << " 个元素为: " << d1.front() << endl;
cout << "第 " << "末" << " 个元素为: " << d1.back() << endl;

7.6 deque容器的排序问题

deque<int>d1;
d1.push_back(10); d1.push_back(20); d1.push_back(30);
d1.push_front(100); d1.push_front(200); d1.push_front(300); 
print(d1);
sort(d1.begin(), d1.end()); //这里需要包含algorithm头文件
print(d1);

7.7 set 和 multiset的区别

set<int>s1;
pair<set<int>::iterator,bool>  ret = s1.insert(10); 
pair<set<int>::iterator, bool> ret1 = s1.insert(10); 
print(s1);
if (ret.second)
{
	cout << "the elem has not been existed!!!" << endl;
	if (ret1.second)
	{
		cout << "the elem has not been existed!!!" << endl;
	}
	else
	{
		cout << "the elem has been existed!!!" << endl;
	}
	}
	else
	{
		cout << "the elem has been existed!!!" << endl;
	}
multiset<int>m1;
m1.insert(10); m1.insert(10); printm(m1);

8 容器案例

8.1 用到的容器的功能

vector<int>v1;			//存放的是第1轮比赛中的参赛人员的编号(12人)
vector<int>v2;			//存放的是第2轮比赛中的参赛人员的编号(6人)
vector<int>vVictory;	//存放的是最终前三名的参赛人员的编号(3人)
map<int, Speecher>m_Speecker;	//存放的是参赛人员的编号和Speaker类(包括姓名成绩)
int m_Index;					//用以表示当前比赛时第几轮
bool fileIsEmpty;				//判断文件是否为空
map<int, vector<string>>m_Record;	//存放往届获奖选手的信息
vector<int>v_Src;				//临时容器,用于代替v1、v2
multimap<double, int, greater<double>>groupScore;//按升序排列小组的成绩第二个是编号(10001)
deque<double>d;				//(队列)存放的是评委打分的10个数据
vector<int>v;			//临时容器用于代替v2和vVictory

8.2 包括的头文件及选手类管理类

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <map>
#include <numeric>
#include <deque>
#include <fstream>
#include <ctime>
using namespace std;

struct Speecher
{
	string m_name;
	double m_score[2];
};
class SpeechManger
{
public:
	SpeechManger()
	{
		InitSpeech();
		CreateSpeecher();
		loadReard();
	}
	~SpeechManger()
	{

	}
	void showmenu()
	{
		cout << "————————————————" << endl;
		cout << "————欢迎参加演讲比赛————" << endl;
		cout << "———— 1.开始演讲比赛 ————" << endl;
		cout << "———— 2.查看往届记录 ————" << endl;
		cout << "———— 3.清空比赛记录 ————" << endl;
		cout << "———— 4.退出比赛程序 ————" << endl;
		cout << "————————————————" << endl;
		cout << endl;
	}
	void Start()
	{
		DrawSpeech();
		ContestSpecch();
		showScore();
		//第二轮比赛
		this->m_Index++;
		DrawSpeech();
		ContestSpecch();
		showScore();
		saveReard();
		//重置
		this->InitSpeech();
		this->CreateSpeecher();
		this->loadReard();
		cout << "本届比赛结束!!" << endl;
		system("pause");
		system("cls");
	}
	vector<int>v1;
	vector<int>v2;
	vector<int>vVictory;
	map<int, Speecher>m_Speecker;
	int m_Index;
	bool fileIsEmpty;
	map<int, vector<string>>m_Record;
};

8.3 主函数

int main()//6
{
	srand((unsigned int)time(NULL));
	int select = 0;
	SpeechManger sm;
	//for (auto iter = sm.m_Speecker.begin(); iter != sm.m_Speecker.end(); iter++)
	//{
	//	cout << iter->first << "  " << iter->second.m_name << "  " << iter->second.m_score[0] << endl;
	//}
	while (1)
	{
		sm.showmenu();
		cout << "请输入您的选择:" << endl;
		cin >> select;
		switch (select)
		{
			//开始
		case 1:sm.Start();
			break;
			//查看往届
		case 2:sm.loadReard();
			sm.showReard();
			break;
			//清空比赛记录
		case 3:sm.clearReard();
			break;
			//退出系统
		case 4:cout << "欢迎下次使用" << endl; system("pause"); exit(0);
			break;
		default:
			system("cls");
			break;
		}
	}
	system("pause");
	return 0;
}

8.4 初始化函数及生成选手函数

void InitSpeech()
{
	this->v1.clear();
	this->v2.clear();
	this->vVictory.clear();
	this->m_Speecker.clear();
	this->m_Record.clear();
	this->m_Index = 1;
}
void CreateSpeecher()
{
	string nameseed = "ABCDEFGHIJKL";
	for (int i = 0; i < nameseed.size(); i++)
	{
		string name = "选手";
		name += nameseed[i];
		Speecher sp;
		sp.m_name = name;
		for (int j = 0; j < 2; j++)
		{
			sp.m_score[j] = 0;		//参赛者的两轮成绩初始化
		}
		this->v1.push_back(i + 10001);	//第一轮比赛的所有蔡赛人员  12人(只存编号)
		this->m_Speecker.insert(make_pair(i + 10001, sp));	//存放一个pair 编号和参赛人员
	}
}

8.5 抽签函数

//抽签
	void DrawSpeech()
	{
		cout << "第《 " << this->m_Index << " 》轮比赛正在抽签中!!!" << endl;
		cout << "———————————-———————————————" << endl;
		cout << "演讲的顺序如下:" << endl;
		if (this->m_Index == 1)
		{
			random_shuffle(this->v1.begin(), this->v1.end());
			for (auto iter = v1.begin(); iter != v1.end(); iter++)
			{
				cout << *iter << "  ";
			}
			cout << endl;
		}
		else
		{
			random_shuffle(this->v2.begin(), this->v2.end());
			for (auto iter = v2.begin(); iter != v2.end(); iter++)
			{
				cout << *iter << endl;
			}
			cout << endl;
		}
		cout << "—————————————————————-—————" << endl;
		system("pause");
		cout << endl;
	}

8.6 开始比赛函数

//比赛
	void ContestSpecch()
	{
		cout << "第《 " << this->m_Index << " 》轮比赛正在进行中!!!" << endl;
		cout << "———————————-———————————————" << endl;
		vector<int>v_Src;
		multimap<double, int, greater<double>>groupScore;//按升序排列小组的成绩
		int num = 0;		//后面有自加,自加到6会进行分组
		if (m_Index == 1)
		{
			v_Src = v1;
		} 
		else
		{
			v_Src = v2;
		}
		for (auto iter = v_Src.begin(); iter != v_Src.end(); iter++)
		{
			num++;
			//评委打分
			deque<double>d;				//(队列)存放的是评委打分的10个数据
			for (int i = 0;i<10;i++)
			{
				double score = (rand() % 401 + 600) / 10.f;			//600-1000
				//cout << score << " ";		//测试
				d.push_back(score);
			}
			sort(d.begin(), d.end(), greater<double>());	//升序排列
			d.pop_back();				//取出最高分
			d.pop_front();				//取出最低分
			double total = accumulate(d.begin(), d.end(), 0.0f);
			double avg = total / (double)d.size();			//平均分
			//cout << avg << "  ";		//平均分
			this->m_Speecker.at(*iter).m_score[this->m_Index - 1] = avg;	//给演讲者的成绩赋值
			//将打分数据放入临时的小组容器
			groupScore.insert(make_pair(avg, *iter));			//在定义时已经设置升序排列(*iter表示的是编号比如10001)
			//每六个人取出前三名
			if (num % 6 == 0)
			{
				cout << "第 " << num / 6 << " 小组比赛名次" << endl;
				for (multimap<double, int, greater<double>>::iterator iter = groupScore.begin(); iter != groupScore.end(); iter++)
				{
					cout << "编号:" << iter->second << "姓名:" << this->m_Speecker[iter->second].m_name
						<< "成绩:" << this->m_Speecker[iter->second].m_score[this->m_Index - 1] << endl;
				}
				//取走前三名的编号
				int count = 0;
				for (auto it = groupScore.begin(); it != groupScore.end() && count<3; it++,count++)
				{
					if (this->m_Index ==1)
					{
						v2.push_back((*it).second);
					} 
					else
					{
						vVictory.push_back((*it).second);
					}
				}
			groupScore.clear();//不请空的话,第二组会有12个人
			cout << endl;
		}
	}
	cout << "第 " << this->m_Index << "轮比赛结束" << endl;;
	cout<<"——————————————————————" << endl;
	system("pause");
}
	

8.7 写读文件函数

void saveReard()
	{
		ofstream ofs;
		ofs.open("speech.csv", ios::out | ios::app);
		for (auto iter = vVictory.begin(); iter != vVictory.end(); iter++)
		{
			ofs << *iter << "," << m_Speecker[*iter].m_score[1] << ",";
		}
		ofs << endl;
		ofs.close();
		cout << "记录已经保存" << endl;
		this->fileIsEmpty = false;
	}
	void loadReard()
	{
		ifstream ifs("speech.csv",ios::in);
		if (!ifs.is_open())
		{
			this->fileIsEmpty = true;
			//cout << "文件不存在!" << endl;
			ifs.close();
			return;
		}
		//文件清空清空
		char ch;
		ifs >> ch;
		if (ifs.eof())
		{
			//cout << "文件为空!!!" << endl;
			this->fileIsEmpty = true;
			ifs.close();
			return;
		}
		//文件存在且不为空
		this->fileIsEmpty = false;
		ifs.putback(ch);
		string data;
		int index = 0;
		while (ifs>>data)
		{
			//cout << data << endl;
			int pos = -1;//查到逗号的位置
			int start = 0;
			vector<string>v;
			while (true)
			{
				pos = data.find(",", start);
				if (pos == -1)
				{
					break;
				}
				string temp = data.substr(start, pos - start);
				//cout << temp << endl;
				v.push_back(temp);				
				start = pos + 1;		
			}	
			m_Record.insert(make_pair(index, v));
			index++;
		}
		ifs.close();
		/*	for (auto iter = m_Record.begin(); iter != m_Record.end(); iter++)
			{
				cout << iter->first << iter->second[0] << iter->second[1] << endl;
			}*/
	}

8.8 显示得分及往届成绩

//显示得分
	void showScore()
	{
		cout << "—————————————————-———" << endl;
		cout << "第 " << this->m_Index << " 轮晋级选手为:" << endl;
		vector<int>v;
		if (this->m_Index == 1)
		{
			v = v2;
		} 
		else
		{
			v = vVictory;
		}
		for (auto iter = v.begin();iter != v.end();iter++)
		{
			cout << "选手编号:" << *iter << "姓名:" << this->m_Speecker[*iter].m_name << "成绩:"
				<< this->m_Speecker[*iter].m_score[this->m_Index - 1] << endl;
		}
		cout << endl;
		system("pause");
		system("cls");
		showmenu();
	}
void showReard()
{
	if (this->fileIsEmpty)
	{
		cout << "文件存在,或者记录为空" << endl;
	}
	for (int i = 0; i < m_Record.size(); i++)
	{
		cout << "第 " << i + 1 << " 届 冠军编号为:" << m_Record.at(i).at(0) << " 得分:"
			<< m_Record.at(i).at(1) << "  "
			<<" 亚军编号为:" << m_Record.at(i).at(2) << " 得分:"
			<< m_Record.at(i).at(3) << "  "
			<<" 季军编号为:" << m_Record.at(i).at(4) << " 得分:"
			<< m_Record.at(i).at(5) << "  " << endl;
	}
	system("pause");
	system("cls");
}

8.9 清空函数

//清空
	void clearReard()
	{
		cout << "您确认要清空所有信息吗?" << endl << "注意:清空后无法恢复" << endl;
		cout << "1:确认" << endl << "2:返回" << endl;
		int select = 0;
		cin >> select;
		if (select == 1)
		{
			ofstream ofs("speech.csv", ios::trunc);
			ofs.close();
//初始化操作
		this->InitSpeech();
		this->CreateSpeecher();
		this->loadReard();
		cout << "清空成功!!" << endl;
	} 
}
" << endl;
		cout << "第 " << this->m_Index << " 轮晋级选手为:" << endl;
		vector<int>v;
		if (this->m_Index == 1)
		{
			v = v2;
		} 
		else
		{
			v = vVictory;
		}
		for (auto iter = v.begin();iter != v.end();iter++)
		{
			cout << "选手编号:" << *iter << "姓名:" << this->m_Speecker[*iter].m_name << "成绩:"
				<< this->m_Speecker[*iter].m_score[this->m_Index - 1] << endl;
		}
		cout << endl;
		system("pause");
		system("cls");
		showmenu();
	}
void showReard()
{
	if (this->fileIsEmpty)
	{
		cout << "文件存在,或者记录为空" << endl;
	}
	for (int i = 0; i < m_Record.size(); i++)
	{
		cout << "第 " << i + 1 << " 届 冠军编号为:" << m_Record.at(i).at(0) << " 得分:"
			<< m_Record.at(i).at(1) << "  "
			<<" 亚军编号为:" << m_Record.at(i).at(2) << " 得分:"
			<< m_Record.at(i).at(3) << "  "
			<<" 季军编号为:" << m_Record.at(i).at(4) << " 得分:"
			<< m_Record.at(i).at(5) << "  " << endl;
	}
	system("pause");
	system("cls");
}

8.9 清空函数

//清空
	void clearReard()
	{
		cout << "您确认要清空所有信息吗?" << endl << "注意:清空后无法恢复" << endl;
		cout << "1:确认" << endl << "2:返回" << endl;
		int select = 0;
		cin >> select;
		if (select == 1)
		{
			ofstream ofs("speech.csv", ios::trunc);
			ofs.close();
//初始化操作
		this->InitSpeech();
		this->CreateSpeecher();
		this->loadReard();
		cout << "清空成功!!" << endl;
	} 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值