标准库的数据结构
stack
#include<iostream>
#include<stack>
using namespace std;
int main() {
stack<int> s;
s.push(3);
s.push(7);
s.push(1);
cout << s.size() << endl;
cout << s.top() << " ";
s.pop();
cout << s.top() << " ";
s.push(5);
cout << s.top() << " ";
return 0;
}
3
1 7 5
总结
size() 返回栈的元素数
top() 返回栈顶的元素
pop() 从栈中取出并删除
push() 从栈中添加元素
empty() 在栈为空时返回true
#include<iostream>
#include<stack>
#include<stdlib.h>
using namespace std;
int main() {
stack<int> S;
int a, b, x;
string s;
while(cin >> s) {
if(s[0] == '+') {
a = S.top();
S.pop();
b = S.top();
S.pop();
S.push(a + b);
} else if(s[0] == '-') {
b = S.top();
S.pop();
a = S.top();
S.pop();
S.push(a - b);
} else if(s[0] == '*') {
a = S.top();
S.pop();
b = S.top();
S.pop();
S.push(a * b);
} else {
S.push(atoi(s.c_str()));
}
}
cout << S.top() << endl;
return 0;
}
1 2 + 3 4 - *
^Z
-3
queue
#include<bits/stdc++.h>
using namespace std;
int main() {
queue<string> Q;
Q.push("red");
Q.push("yellow");
Q.push("yellow");
Q.push("blue");
// red
cout << Q.front() << " " << endl;
Q.pop();
// yellow
cout << Q.front() << " " << endl;
Q.pop();
// yellow
cout << Q.front() << " " << endl;
Q.pop();
Q.push("green");
// blue
cout << Q.front() << " " << endl;
Q.pop();
// green
cout << Q.front() << " " << endl;
return 0;
}
size() 返回队列的元素数
front() 返回队头的元素
pop() 从队列中取出并删除元素
push() 向队列中添加元素x
empty() 在队列为空时返回true
#include<bits/stdc++.h>
using namespace std;
int main() {
int n, q, t;
string name;
queue<pair<string, int> > Q;
cin >> n >> q;
for(int i = 0; i < n; i++) {
cin >> name >> t;
Q.push(make_pair(name, t));
}
pair<string, int> u;
int elaps = 0, a;
while(!Q.empty()) {
u = Q.front();
Q.pop();
a = min(u.second, q);
u.second -= a;
elaps += a;
if(u.second > 0) {
Q.push(u);
} else {
cout << u.first << " " << elaps << endl;
}
}
return 0;
}
5 100
p1 150
p2 80
p3 200
p4 350
p5 20
p2 180
p5 400
p1 450
p3 550
p4 800
vector
#include<bits/stdc++.h>
using namespace std;
void print(vector<double> V) {
int vlen = V.size();
for(int i = 0; i < vlen; i++) {
cout << V[i] << " ";
}
cout << endl;
}
int main() {
vector<double> V;
V.push_back(0.1);
V.push_back(0.2);
V.push_back(0.3);
V[2] = 0.4;
print(V);
V.insert(V.begin() + 2, 0.8);
print(V);
V.erase(V.begin() + 1);
print(V);
V.push_back(0.9);
print(V);
return 0;
}
size() 返回向量的元素数
push_back(x) 在向量末尾添加元素x
pop_back() 删除向量的最后一个元素
begin() 返回指向向量开头的迭代器
end() 返回指向向量末尾(最后一个元素的后一个位置)的迭代器
insert(p, x) 在向量的位置p处插入元素x
erase§ 删除向量中位置p的元素
clear() 删除向量中所有元素
list
#include<bits/stdc++.h>
using namespace std;
int main() {
// 双向链表list
list<char> L;
// [b]
L.push_front('b');
// [bc]
L.push_back('c');
// [abc]
L.push_front('a');
// a
cout << L.front();
// c
cout << L.back();
// [bc]
L.pop_front();
// [bcd]
L.push_back('d');
// b
cout << L.front();
// d
cout << L.back();
return 0;
}
size() 返回表的元素数
begin() 返回指向表开头的迭代器
end() 返回指向表末尾(最后一个元素的后一个位置)的迭代器
push_front(x) 在表的开头添加元素x
push_back(x) 在表的末尾添加元素x
pop_front() 删除位于表开头的元素
pop_back() 删除位于表结尾的元素
insert(p, x) 在表的位置p处插入x
erase§ 删除表中p的元素
clear() 删除表中所有的元素
#include<bits/stdc++.h>
using namespace std;
int main() {
int q, x;
char com[20];
// 双向链表list
list<int> v;
cin >> q;
for(int i = 0; i < q; i++) {
cin >> com;
if(com[0] == 'i') {
cin >> x;
v.push_front(x);
} else if(com[6] == 'L') {
v.pop_back();
} else if(com[6] == 'F') {
v.pop_front();
} else {
cin >> x;
list<int>::iterator it;
for(it = v.begin(); it != v.end(); it++) {
if(*it == x) {
v.erase(it);
break;
}
}
}
}
int i = 0;
for(list<int>::iterator it = v.begin(); it != v.end(); it++) {
if(i++) {
cout << " ";
cout << *it;
}
cout << endl;
}
return 0;
}