STL rope

发布时间 2023-05-24 20:50:51作者: Optima_Xu

头文件:#include <ext/rope>
命名空间:using namespace __gnu_cxx

rope test;

test.push_back(x);//在末尾添加x

ps:注意当test为rope<char>类型时只能添加单个字符而不能是字符串。

test.insert(pos,x);//在pos插入x  

test.erase(pos,x);//从pos开始删除x个

test.copy(pos,len,x);//将pos开始长len个元素替换到x中

ps:注意当test为rope<char>类型时x只能为char[]而不能是string。

test.replace(pos,x);//将pos换成x

test.substr(pos,x);//提取pos开始x个

test.at(x)/[x];//访问第x个元素

test.clear();//清空元素  

rope 内部是块状链表实现的,黑科技是支持 O(1)
复制,而且不会空间爆炸 (rope 是平衡树,拷贝时只拷贝根节点就行)。因此可以用来做可持久化数组。

拷贝历史版本的方式:

rope<int> *his[100000];
his[i] = new rope<int> (*his[i - 1]);

缺点是常数大 (C++ STL 的通病)。

还有一个叫 crope 的东西,crope 即 rope,可以用 cin/cout 直接输入输出,常用于字符串操作。

本文整理自网上资料。