stl
作者:
inuyasha
,
2020-10-05 00:59:28
,
所有人可见
,
阅读 602
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
/*
vector:变长数组,倍增
string:字符串 substr(), c_str()
queue:push(), front(), pop()
priority_queue:push(), front(), pop();
stack:push(), pop()
deque:双端队列
set, map,multiset, multimap:基于红黑树,动态维护有序序列。
unordered_multiset, unordered_multimap:基于哈希表
bitset:压位。
*/
int main() {
vector<int> a1(10);
for(int n : a1) cout << n << " ";
cout << endl;
vector<int> a2(10, 3);
for(int n: a2) printf("%d ", n);
puts("");
for(vector<int>::iterator i=a2.begin(); i!=a2.end(); i++) cout << *i << " ";
puts("");
cout << "a2.size() == " << a2.size() << endl;
cout << "a2.empty() == " << a2.empty() << endl;
a2.clear();
puts("");
pair<int, string> p;
p.first = 1;
p.second = "aaa";
cout << p.first << endl;
cout << p.second;
p = make_pair(222,"222");
p = {22,"xxx"};
string str = "aaa";
cout << str.size() << str.empty() << endl;
str += "sss";
string sub = str.substr(2,2); // 从第2个字母开始返回2个字符,超出则返回到最后一个字符
cout << sub;
puts("");
queue<int> q;
q.push(2);
q.pop();
q.size();
cout << "empty? : " << q.empty() << endl;
q.front();
q.push(22);
cout << "qback: " << q.back() << endl;
cout << "qfront: " << q.front() << endl;
priority_queue<int> heap;
heap.push(2);
heap.push(4);
while(heap.size() > 0) cout << heap.top() << " ", heap.pop();
puts("");
priority_queue<int, vector<int>, greater<int>> heap2;
heap2.push(2);
heap2.push(4);
while(heap2.size()>0) {
cout << heap2.top() << " ";
heap2.pop();
}
puts("");
set<int> s;
s.insert(1);
s.insert(3);
s.count(1);
s.count(2);
multiset<int> ms;
ms.insert(1);ms.insert(1);
cout << ms.count(1) << endl;
ms.find(1);
cout << *ms.lower_bound(2);// 大于x的最小数的迭代器
ms.erase(2);
map<int, int> m;
m[2]=3;
}