stl代码练习

learn_stl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180

#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
#define MAX_SIZE 10000

using namespace std;

int n;
typedef struct node{
string name;
//string暂时可以当成一种结构体变量,专门用来定义字符串,name的长度可以由name.length()获得
int grade;
}Student;

vector<int> vec;
list<int> link_list;
deque<int> dq;
stack<int> Stack;
queue<int> que;
priority_queue<int> q;
set<int> s;
map<string,int> stu;

void fun_1(int a[]);
void fun_2(int a[]);
void fun_3(int a[]);
void fun_4(int a[]);
void fun_5(int a[]);
void fun_6(int a[]);
void fun_7(Student a[]);

int main(){
int a[MAX_SIZE];
Student student[MAX_SIZE];
cin >> n;
for(int i = 0; i < n; i++) cin >> a[i];
for(int i = 0; i < n; i++) cin >> student[i].name >> student[i].grade;//只用于map


/*

1.用vector存入数组中所有的数据并升序排序,然后删去最大元素
*/

for(int i = 0; i < n; i++)
vec.push_back(a[i]);
sort(vec.begin(), vec.end());
vec.pop_back();
for(int i = 0; i < vec.size(); i++)
cout << vec[i] << endl;


/*
2. 用list存入数组中所有的元素并升序排序。删除其中最小的节点并再插入一个最大的节点
*/


for(int i = 0; i < n; i++)
link_list.push_back(a[i]);
link_list.sort();

link_list.erase(link_list.begin());
link_list.insert(link_list.begin(), *(--link_list.end()));
for(list<int>::iterator itt = link_list.begin(); itt != link_list.end(); itt++)
cout << *itt << endl;



/*
3.用deque存入数组中所有的数据,在头尾端插入一个和队头队尾相同的元素。
将deque中的数据用另一个vector存储起来排序后再放回deque中
*/


for(int i = 0; i < n; i++)
dq.push_back(a[i]);
dq.push_back(a[n-1]), dq.push_front(a[0]);
vector<int> haha;
for(int i = 0; i < n; i++)
haha.push_back(a[i]);
sort(haha.begin(), haha.end());
dq.clear();
deque<int> dq(haha.begin(), haha.end());
for(deque<int>:: iterator ittt = dq.begin(); ittt != dq.end(); ittt++)
cout << *ittt << endl;


/*
4.将数组中的元素依次存入stack中,然后依次存入queue中实现数组元素的reverse
*/


for(int i = 0; i < n; i++)
Stack.push(a[i]);
for(int i = n-1; i >= 0; i--)
{
que.push(Stack.top());
Stack.pop();
}
for(int i = n - 1; i >= 0; i--)
cout << que.front() << endl;


/*
5.不借助其他的函数和变量,利用priority_queue实现数组的升序排序
*/


priority_queue<int, vector<int>, less<int> > q;
for(int i = 0; i < n; i++)
q.push(a[i]);
for(int i = 0; i < n; i++)
cout << q.top() << endl;



/*
6.将数组中的元素全部存入set中,并输入一个元素,在set中查找它,找到则删除它
*/


for(int i = 0; i < n; i++)
s.insert(a[i]);
int j;
scanf("%d", &j);
set<int>::iterator itt;
itt = s.find(j);
if(itt != s.end())
s.erase(itt);
for(itt = s.begin(); itt != s.end(); itt++)
cout << *itt << endl;


/*
7.将学生的姓名和成绩全部存入map中,并根据学生成绩降序排序。

(不能修改给定map的两个参数的顺序)
*/

//第一种


int cmp(pair <string, int> a, pair<string, int> b);
pair<string, int> pa;
for(int i = 0; i < n; i++)
pa = make_pair(student[i].name, student[i].grade);
for(int i = 0; i < n; i++)
stu.insert(pair<string, int>(student[i].name, student[i].grade));
vector<pair<string, int> > baba(stu.begin(), stu.end() );
sort(baba.begin(), baba.end(), cmp);
for(int i = 0; i < baba.size(); i++)
cout << baba[i].first << " " << baba[i].second << endl;


//第二种(就是直接用make_pair插入以及sort函数直接用greater逆序排列)


for(int i = 0; i < n; i++)
stu.insert(make_pair(student[i].name, student[i].grade));
vector<pair<string, int> > yeye(stu.begin(), stu.end() );
sort(yeye.begin(), yeye.end(), greater<pair<string, int> >());
for(int i = 0; i < n; i++)
cout << yeye[i].first << " " << yeye[i].second << endl;


//分割线


return 0;
}
bool cmp(pair <string, int> a, pair<string, int> b)
{
return a.second > b.second;
}
------ The Happy Ending ------