扩展库

  1. STL——rope
    头文件 :#include<ext/rope>
    命名空间: using namespace __gnu_cxx;
    定义方法:rope<变量类型>变量名称; 或 crope 变量名称; 其中crope相当于定义成rope,即定义为string类型
    人话解释:超级string
    算法解释:块状链表(即讲链表与数组的优势结合,形成分块思想)
    用途解释:这本来是一个用于快速操作string的工具,却一般被定义成int,然后用作可持久化线段树!

操作:

  • insert(int pos, string &s, int n) 将字符串s的前n位插入rope的下标pos处,如没有参数n则将字符串s的所有位都插入rope的下标pos处
  • append(string &s,int pos,int n) 把字符串s中从下标pos开始的n个字符连接到rope的结尾,如没有参数n则把字符串s中下标pos后的所有字符连接到rope的结尾,如没有参数pos则把整个字符串s连接到rope的结尾
  • substr(int pos, int len) 提取rope的从下标pos开始的len个字符
  • at(int x) 访问rope的下标为x的元素
  • erase(int pos, int num) 从rope的下标pos开始删除num个字符
  • copy(int pos, int len, string &s) 从rope的下标pos开始的len个字符用字符串s代替,如果pos后的位数不够就补足
  • replace(int pos, int *x);//从rope的下标pos开始替换成数组x,x的长度为从pos开始替换的位数,如果pos后的位数不够就补足
  • push_back(x)在末尾添加x

大佬博客

  1. pb_ds
    头文件: #include <ext/pb_ds/assoc_container.hpp>
    命名空间:using namespace __gnu_pbds;

优先队列:#include <ext/pb_ds/priority_queue.hpp>
定义: __gnu_pbds::priority_queue<int,greater<int>,pairing_heap_tag> pq;
因为重名的原因一定要加上 __gnu_pbds::

__gnu_pbds::priority_queue<int> q,p;
q.push(1);
cout << q.top();
p.push(5);
p.join(q);
cout << (q.empty() ? "空" : "不空") << endl;
while(!p.empty()) {
    
    
	cout << p.top() << endl;
	p.pop();
}

名次数/红黑树:#include <ext/pb_ds/tree_policy.hpp>
定义:tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update> t;
注意,插入的元素会去重,如set

操作:

  • null_type为映射类型, 低版本g++为 null_mapped_type
  • less<int>, greater<int> 比较器
  • rb_tree_tag 和 splay_tree_tag 选择树的类型
  • tree_order_statistics_node_update 结点更新
  • insert, erase
  • order_of_key rank 返回值
  • find_by_order() kth 返回迭代器
  • lower_bound() 前继, >=x 最小的迭代器
  • upper_bound() 后继 >x 最小的迭代器
  • a.join(b) b并入a,前提是两颗树的取值范围不相交
  • a.split(v, b) key <= v的属于a,其他属于b

哈希表:#include<ext/pb_ds/hash_policy.hpp>
定义:cc_hash_table<string,int>mp1;//拉链法
gp_hash_table<string,int>mp2;//查探法(快一些)

  1. 正则
    regex_search 只会搜索一次
string str = "Hello 2018, Bye 2017";
smatch result;
regex pattern("\\d{4}");	//匹配四个数字

//迭代器声明
string::const_iterator iterStart = str.begin();
string::const_iterator iterEnd = str.end();
string temp;
while (regex_search(iterStart, iterEnd, result, pattern)) {
    
    
	temp = result[0];
	cout << temp << " ";
	iterStart = result[0].second;	//更新搜索起始位置,搜索剩下的字符串
}
//	输出结果:2018 2017

猜你喜欢

转载自blog.csdn.net/qq_41829380/article/details/110792713