STL中的基本容器:
vector : 变长数组 , 倍增的思想(由于系统为某一程序分配空间时,所需时间与空间大小无关,与申请次数有关,所以每次增加数组长度时会增加一倍(空间换时间))
pair : 存储一个二元组
string : 字符串 , substr() , c_str()
queue : 队列 , push() , front() , pop()
priority_queue : 优先队列 ,默认是大根堆, push() , top() , pop()
stack : 栈 , push() , top() , pop()
deque : 双端队列(不常用,效率比一般的数组慢好几倍)
set , map , multiset , multimap : 基于平衡二叉树(红黑树),动态维护有序序列
unordered_set , unordered_map , unordered_multiset , unordered_multimap : 哈希表
bitset : 压位
next_permutation:排列
用到的头文件:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
基本用法:
一、vector
#include <vector>
int main()
{
vector<int> a , a(10) , a[10] , a(10,3);
a.size(); //返回元素个数 ,O(1)
a.empty(); // 返回是否为空 , O(1)
a.clear(); // 清空
vector<int>::iterator it; //迭代器
类似指针 *it 表示 a[0] , *(it+2) 表示 a[2]
a.front()/a.back(); // 返回第一个/最后一个数
a.push_back()/a.pop_back(); // 在最后插入一个数/删掉最后一个数
a.begin()/a.end(); // 返回第0个数/最后一个数后面的一个数
*a.begin() = a[0]
a[8] , a[84]; // 随机取址
遍历:
一:
for(int i = 0 ; i < 10 ; i ++) a.push_back(i);
for(int i = 0 ; i < a.size() ; i ++) cout << a[i] << ' ' ;
cout << endl;
二:迭代器:可以理解为指针,所以需要用*来取出值
for(vector<int>::iterator i = a.begin() ; i != a.end() ; i ++ ) cout << *i << ' ';
cout << endl;
三:
for(auto x : a) cout << x << ' ';
cout << endl;
支持比较运算(按字典序):
vector<int> a(4,3) , b(3,4);
if(a < b) puts("a < b");
return 0;
}
二、pair
int main()
{
//创建和初始化:
pair<string , pair<int , int> > P; //<>里面可以是任意类型的两个变量
pair<int , string> p(10,"abc");
pair<int , string> p1,p2;
p1 = make_pair(10 , "abc");
p2 = p1;
p2.first; // 第一个元素
p2.second; // 第二个元素
cout << p2.first << p2.second << endl;
支持比较运算,以first为第一关键字,second为第二关键字(按字典序)
return 0;
}
三、string
int main()
{
string s;
s.size(); //返回字符个数
s.empty(); //返回字符串是否为空
s.clear(); // 清空字符串
支持加法运算(每个加法运算都有一个运算符是string):
s += "abc";
s += 'c';
s.substr(1,2); //返回某段字符串,第一个参数表示从这一位置(1)开始,第二个参数表示返回字符串长度(2)
s.substr(1); // 省略第二个参数,返回从1(第一个参数)开始到末尾的字符串
输出:
cout << s << endl;
printf("%s\n",s.c_str());
return 0;
}
四、queue
#include <queue>
int main()
{
queue<int> q;
q.size() , q.empty();
q.push(5); // 向队尾插入一个元素
q.front(); // 返回队头元素
q.back(); // 返回队尾元素
q.pop(); // 弹出队头元素
q = queue<int>(); // 清空只需要重新定义一下
return 0;
}
五、priority_queue
#include <queue>
#include <vector>
int main()
{
priority_queue<int> h; //默认大根堆
定义成小根堆的方式:
priority_queue<int , vector<int> , greater<int> > q;
如果定义一个结构体类型的优先队列大根堆必须要重载一个小于号,小根堆重载大于号
struct rec
{
int a, b;
bool operator< (const rec& t) const
{
return a < t,a;
}
};
priority_queue<rec> d;
struct Rec
{
int a, b;
bool operator> (const rec& t) const
{
return a > t,a;
}
};
priority_queue<Rec , vector<int> , greater<int> > q;
h.push(8);//插入一个元素
h.top();//返回堆顶元素
h.pop();//弹出堆顶元素
return 0;
}
六、stack
#include <stack>
int main()
{
stack<int> s;
s.size(),s.empty();
s.push(4); // 向栈顶插入一个元素
s.top(); //返回栈顶元素
s.pop(); //弹出栈顶元素
return 0;
}
七、deque(不常用,效率低)
#include <deque>
int main()
{
deque<int> q;
q.size(),q.empty();
q.front()/q.back(); //返回第一个/最后一个元素
q.push_back()/q.pop_back(); //向最后插入一个元素/弹出最后一个元素
q.push_front()/q.pop_front(); //从队首插入/弹出队首元素
q.begin()/q.end(); //迭代器
q.clear();
q[5],q[8];//随机取址
return 0;
}
八、set、multiset、map、multimap(自动排序)
#include <set>
#include <map>
int main()
{
增删改查的操作时间复杂度都是O(logn)
int x;
public:
size();
empty();
clear();
begin()/end(); //支持++,--操作,返回前驱和后继,O(logn)
set<int>::iterator it = a.begin();
it ++ , it --;
++ it , -- it;
set<int> s/multiset<int> ms;
//set里面不允许有重复的数
//set定义结构体类型必须重载小于号
insert(x); //插入一个数(6)
find(x); //查找一个数(4),不存在返回s.end()
count(x); //返回某个数(5)的个数
erase();//(1) 输入是一个数x,删除所有x O(k+logn),k是x的个数
//(2)输入是一个迭代器,删除迭代器
lower_bound(x); //返回大于等于x的最小的数的迭代器
upper_bound(x); //返回大于x的最小的数的迭代器
map<string,int>a/multimap<string,int>ua;
insert(x); //插入的数是一个pair
erase(); //输入的参数是pair或者迭代器
find(x);
a["abc"] = 1; //可以像数组一样用,O(logn)
lower_bound(x)/upper_bound(x);
return 0;
}
九、unordered_set , unordered_map , unordered_multiset , unordered_multimap(不会自动排序)
#include <unordered_set>
#include <unordered_map>
int main()
{
增删改查的时间复杂度都是O(1)
int x;
public:
size();
empty();
clear();
unordered_set<int> s/unordered_multiset<int> ms; //set里面不允许有重复的数
insert(x); //插入一个数(6)
find(x); //查找一个数(4),不存在返回s.end()
count(x); //返回某个数(5)的个数
erase();//(1) 输入是一个数x,删除所有x
//(2)输入是一个迭代器,删除迭代器
unordered_map<string,int>a/unordered_multimap<string,int>ua;
insert(x); //插入的数是一个pair
erase(); //输入的参数是pair或者迭代器
find(x);
a["abc"] = 1; //可以像数组一样用
return 0;
}
十、bitset
#include <bitset>
int main()
{
bitset<10000> s;
支持所有位运算:
~ , & , | , ^ , >> , << , == , != , []
count(); //返回有多少个1
any(); //判断是否至少有一个1
none(); // 判断是否全为0
set(); // 把所有位置成1、
set(k,v); // 将第k为变成v
reset(); // 把所有位变成0
flip(); // 等价于~
flip(k) ; // 把第k位取反
return 0;
}
十一、next_permutation
next_permutation返回传入数组的下一个排列(按字典序从小到大),返回的是最大序列时返回false。时间复杂度O(n)。
求数字数组全排列
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<vector<int> > permutation(vector<int>& nums)
{
sort(nums.begin() , nums.end());
vector<vector<int> > res;
do res.push_back(nums); while(next_permutation(nums.begin() , nums.end()));
return res;
}
int main()
{
vector<int> a ({1,2,3});
vector<vector<int> > ans = permutation(a);
for(vector<int> x : ans)
{
for(int y : x) cout << y << ' ';
cout << endl;
}
return 0;
}
输出:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1