一、序:STL容器通用命令
定义STL容器:容器名<元素类型> 名称 eg. vector<int> a
push(元素)增加一个元素
pop(元素)弹出一个元素
size()获取元素个数
empty()判断是否为空
二、常用容器
1.向量vector 【动态数组】
push_back(元素)增加一个元素到最后
pop_back(元素)弹出最后一个元素
insert(位置,元素)插入元素到指定位置
erase(位置)删除指定位置元素
front()读取第一个元素
back()读取最后一个元素
begin()返回第一个元素位置的指针
end()返回最后一个元素位置的指针
resize(大小)重新设定可保存的元素数量
2.栈stack 【先进后出】
P1056 - 火车轨道 - ETOJ (eriktse.com)
top(元素)读取栈顶元素
#include<bits/stdc++.h>
using namespace std;
#define qio ios::sync_with_stdio(0), cin.tie(0),cout.tie(0);
const int N = 1e5 + 10;
int a[N];
int main() {
qio
int n;
cin >> n;
for (int i = 1; i <= n; i++)cin >> a[i];
stack<int> stk;
int pos = 1;
for (int i = 1; i <= n; i++) {
while (pos <= n && (stk.empty() || stk.top() != i))stk.push(a[pos++]);
if (stk.top() == i) stk.pop();
else {
cout << "No";
return 0;
}
}
cout << "Yes";
}
3.队列queue 【先进先出】
front()读取队首元素
back()读取队尾元素
4.优先队列priority_queue
P1057 - 小e的菜篮子 - ETOJ (eriktse.com)
默认为大根堆,会将堆中最大的元素放到栈顶,剩下的顺序无规律
定义比较方式:
返回大值
struct cmp {
bool operator()(const int &u, const int &v) {
return u > v;
}
};
#include<bits/stdc++.h>
using namespace std;
#define qio ios::sync_with_stdio(0), cin.tie(0),cout.tie(0);
typedef long long ll;
int main(){
qio
int q;
cin >> q;
priority_queue<int> pq;
ll sum = 0;
while (q--) {
int t; cin >> t;
if (t == 1) {
int x; cin >> x;
pq.push(x);
sum += x;
} else if (sum != 0) {
sum -= pq.top();
pq.pop();
}
}
cout << sum;
}
5.映射map
P1052 - 气球数量 - ETOJ (eriktse.com)
实现两个元素一一对应的数据结构,并以一定的顺序排列
定义方法:map<关键字,数值> 名称
例如map<string, int > mp 实现了字符串(名称)与数值的一一对应
mp.count(s) 寻找mp中有没有s这个元素,返回0或1
mp.find(s)寻找mp中有没有s这个元素,返回s的iterator迭代器(位置),没有则返回end()
#include<bits/stdc++.h>
using namespace std;
#define qio ios::sync_with_stdio(0), cin.tie(0),cout.tie(0);
void solve();
int main() {
qio
int T;
cin >> T;
while (T--)solve();
}
void solve() {
int n; cin >> n;
vector<string> v;
map<string, int> mp;
for (int i = 1; i <= n; i++) {
string s; cin >> s;
if (mp.count(s)) {
mp[s]++;
}
//if (mp.find(s) != mp.end())
else {
v.push_back(s);
mp[s] = 1;
}
}
for (auto &i : v) {
cout << i << ' ' << mp[i] << '\n';
}
}
6.集合set
P1085 - 【模板】排序(1) - ETOJ (eriktse.com)
线性顺序结构{value1, value2, value3, value4, ……}根据value排序(升序),且value唯一
insert()插入,erase()删除(iter)
可用于①去重②存在性检验③存图 ④快速插入删除查找⑤lower_bound,upper_bound
定义方法:set<int> st
#include<bits/stdc++.h>
using namespace std;
#define qio ios::sync_with_stdio(0), cin.tie(0),cout.tie(0);
int main() {
qio
int n; cin >> n;
set<int> st;
for (int i = 0; i < n; i++) {
int x; cin >> x;
st.insert(x);
}
for (auto &i : st) cout << i << ' ';
}
7.位集合bitset
P1054 - 数的种类 - ETOJ (eriktse.com)
像一个有很多位的二进制数(只能填0或1),支持位运算,但不支持加减乘除
定义方法:bitset<位数> bs
count()返回1的个数
reset()全部重置为0
[ ]当作bool数组
#include<bits/stdc++.h>
using namespace std;
#define qio ios::sync_with_stdio(0), cin.tie(0),cout.tie(0);
const int N = 5e3 + 10;
const int M = 5e5 + 10;
int a[N];
int main() {
qio
int n; cin >> n;
bitset<M> bs;
bs[0] = 1;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) bs |= (bs << a[i]);
cout << bs.count();
}
三、STL函数
1.二分查找函数binary_search
binary_search(开始位置,结束位置,目标元素),返回true或者false
2.二分查找函数lower_bound
lower_bound(开始位置,结束位置,目标元素),返回第一个大于等于指定数的位置(迭代器),如果没找到,返回最后一个数据后面的位置。
upper_bound(开始位置,结束位置,目标元素),返回第一个大于指定数的位置(迭代器),如果没找到,返回最后一个数据后面的位置。
#include<bits/stdc++.h>
using namespace std;
#define qio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
typedef long long ll;
typedef double db;
void solve();
signed main() {
qio
int T = 1;
cin >> T;
while (T--)solve();
}
const int N = 2e5 + 10;
multiset<ll> st;
void solve() {
int op;
cin >> op;
if (op == 1) {
ll x;
cin >> x;
st.insert(x);
} else if (op == 2) {
ll x, k, n;
cin >> x >> k;
n = k;
auto ans = st.upper_bound(x);
// 1 1 3 3 3 5
for (int i = 1; i <= n; i++) {
if (ans == st.begin() || k == 0)break;
ans--, k--;
}
if (k)cout << -1 << '\n';
else cout << *ans << '\n';
} else if (op == 3) {
ll x, k, n;
cin >> x >> k;
n = k;
auto ans = st.lower_bound(x);
for (int i = 1; i <= n; i++) {
if (ans == st.end() || k <= 1 )break;
ans++, k--;
}
if (ans == st.end()) cout << -1 << '\n';
else cout << *ans << '\n';
}
}
3.堆操作函数
make_heap(开始位置,结束位置) 对一段数组或向量建堆,默认大根堆
push_heap(开始位置,结束位置)
pop_heap(开始位置,结束位置)
sort_heap(开始位置,结束位置) 递增排序堆中元素