绳索是可伸缩的字符串实现。它们专为涉及整个字符串的高效操作而设计。诸如赋值、连接和子字符串之类的操作花费的时间几乎与字符串的长度无关。
绳索是一棵二叉树,其中每个叶子(末端节点)都包含一个字符串和一个长度(也称为“权重”),而树更靠后的每个节点保存其左子树中所有叶子的长度之和.具有两个孩子的节点因此将整个字符串分成两部分:左子树存储字符串的第一部分,右子树存储字符串的第二部分,节点的权重是第一部分的长度。
对于绳索操作,在典型的非破坏性情况下,节点中存储的字符串被假定为常量不可变对象,从而允许一些写时复制行为。叶节点通常实现为基本的固定长度字符串,并在不再需要时附加引用计数以进行释放,尽管也可以使用其他垃圾收集方法。
注意: rope 类和rope header 是SGI 扩展;它们不是 C++ 标准库的一部分。
宣言:
绳索的定义方式与向量相同,如“vector
以下是相同的程序:
方案一:
C++
// C++ program to illustrate the use
// of ropes using Rope header file
#include
#include
// SGI extension
using namespace __gnu_cxx;
using namespace std;
// Driver Code
int main()
{
// rope r = "abcdef"
crope r = "abcdef";
cout << r << "\n";
return 0;
}
C++
// C++ program to illustrate the use
// of ropes using Rope header file
#include
#include
// SGI extension
using namespace __gnu_cxx;
using namespace std;
// Driver Code
int main()
{
// rope r = "abcdef"
crope r = "geeksforgeeks";
cout << "Initial rope: "
<< r << endl;
// 'g' is added at the
// end of the rope
r.push_back('g');
r.push_back('f');
r.push_back('g');
cout << "Rope after pushing f: "
<< r << endl;
int pos = 2;
// gfg will be inserted
// before position 2
r.insert(pos - 1, "gfg");
cout << "Rope after inserting "
<< "gfg at positon 2: " << r
<< endl;
// gfg will be deleted
r.erase(pos - 1, 3);
cout << "Rope after removing gfg"
<< " inserted just before: "
<< r << endl;
// Replace "ee" with "00"
r.replace(pos - 1, 2, "00");
cout << "Rope after replacing "
<< "characters: " << r
<< endl;
// Slice the rope
crope r1 = r.substr(pos - 1, 2);
cout << "Subrope at position 2: "
<< r << endl;
// Removes the last element
r.pop_back();
r.pop_back();
r.pop_back();
cout << "Final rope after poping"
<< " out 3 elements: " << r;
return 0;
}
C++
// C++ program to illustrate the use
// of ropes using Rope header file
#include
#include
// SGI extension
using namespace __gnu_cxx;
using namespace std;
// Driver Code
int main()
{
// rope r = "abcdef"
crope r = "abcdef";
cout << r.size() << endl;
cout << r.max_size() << endl;
return 0;
}
C++
// C++ program to illustrate the use
// of ropes using Rope header file
#include
#include
// SGI extension
using namespace __gnu_cxx;
using namespace std;
// Driver Code
int main()
{
// rope r = "abcdef"
crope r = "abcdef";
rope::iterator it;
for (it = r.mutable_begin();
it != r.mutable_end(); it++) {
// Print the value
cout << char((*it) + 2)
<< "";
}
return 0;
}
输出:
abcdef
绳子上允许的操作:
- push_back():该函数用于在绳子的末端输入一个字符。时间复杂度: O(log N)。
- pop_back():从 C++11 引入(对于字符串),该函数用于删除绳子的最后一个字符。时间复杂度: O(log N)。
- insert(int x,crope r1):在第x个元素之前插入r1的内容。时间复杂度:对于最佳情况: O(log N) 和对于最坏情况: O(N)。
- erase(int x, int l):擦除 l 个元素,从第x个元素开始。时间复杂度: O(log N)。
- substr(int x, int l):返回一个新的绳子,它的元素是从位置x开始的l 个字符。时间复杂度: O(log N)。
- 替换(INT X,INT升,crope R1):替换为开头的升元件X个元件与在R1的元素。时间复杂度: O(log N)。
- concatenate(+):使用“+”符号连接两条绳子。时间复杂度: O(1)。
以下是相同的程序:
方案二:
C++
// C++ program to illustrate the use
// of ropes using Rope header file
#include
#include
// SGI extension
using namespace __gnu_cxx;
using namespace std;
// Driver Code
int main()
{
// rope r = "abcdef"
crope r = "geeksforgeeks";
cout << "Initial rope: "
<< r << endl;
// 'g' is added at the
// end of the rope
r.push_back('g');
r.push_back('f');
r.push_back('g');
cout << "Rope after pushing f: "
<< r << endl;
int pos = 2;
// gfg will be inserted
// before position 2
r.insert(pos - 1, "gfg");
cout << "Rope after inserting "
<< "gfg at positon 2: " << r
<< endl;
// gfg will be deleted
r.erase(pos - 1, 3);
cout << "Rope after removing gfg"
<< " inserted just before: "
<< r << endl;
// Replace "ee" with "00"
r.replace(pos - 1, 2, "00");
cout << "Rope after replacing "
<< "characters: " << r
<< endl;
// Slice the rope
crope r1 = r.substr(pos - 1, 2);
cout << "Subrope at position 2: "
<< r << endl;
// Removes the last element
r.pop_back();
r.pop_back();
r.pop_back();
cout << "Final rope after poping"
<< " out 3 elements: " << r;
return 0;
}
输出:
Initial rope: geeksforgeeks
Rope after pushing f: geeksforgeeksgfg
Rope after inserting gfg at positon 2: ggfgeeksforgeeksgfg
Rope after removing gfg inserted just before: geeksforgeeksgfg
Rope after replacing characters: g00ksforgeeksgfg
Subrope at position 2: g00ksforgeeksgfg
Final rope after poping out 3 elements: g00ksforgeeks
容量功能:
- size():返回绳子的长度。
- max_size():保证可表示的最长绳索的尺寸。
以下是相同的程序:
方案三:
C++
// C++ program to illustrate the use
// of ropes using Rope header file
#include
#include
// SGI extension
using namespace __gnu_cxx;
using namespace std;
// Driver Code
int main()
{
// rope r = "abcdef"
crope r = "abcdef";
cout << r.size() << endl;
cout << r.max_size() << endl;
return 0;
}
输出:
6
1836311902
迭代器:
- mutable_begin():返回一个指向绳子开头的迭代器。
- mutable_end():返回一个指向绳索末端的迭代器。
以下是相同的程序:
程序4:
C++
// C++ program to illustrate the use
// of ropes using Rope header file
#include
#include
// SGI extension
using namespace __gnu_cxx;
using namespace std;
// Driver Code
int main()
{
// rope r = "abcdef"
crope r = "abcdef";
rope::iterator it;
for (it = r.mutable_begin();
it != r.mutable_end(); it++) {
// Print the value
cout << char((*it) + 2)
<< "";
}
return 0;
}
输出:
cdefgh
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live