绳索是可伸缩的字符串实现。它们专为涉及整个字符串的高效操作而设计。赋值,串联和子字符串之类的操作所花费的时间几乎与字符串的长度无关。
绳子是一棵二叉树,其中每个叶子(末端节点)都包含一个字符串和一个长度(也称为“权重”),而树的每个节点都包含其左子树中所有叶子的长度之和。 。因此,具有两个子节点的节点将整个字符串分为两部分:左子树存储字符串的第一部分,右子树存储字符串的第二部分,节点的权重是第一部分的长度。
对于绳索操作,在典型的非破坏性情况下,假定存储在节点中的字符串是恒定不变的对象,从而允许某些写时复制行为。叶子节点通常被实现为基本的定长字符串,并在不再需要时附加了用于释放的引用计数,尽管也可以使用其他垃圾回收方法。
注意:绳索类和绳索头是SGI扩展;它们不是C++标准库的一部分。
宣言:
绳索的定义方式与向量的定义相同,即“ vector
以下是相同的程序:
程序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 = "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)。
- delete(int x,int l):擦除l个元素,从第x个元素开始。时间复杂度: O(log N)。
- substr(int x,int l):返回一条新的绳索,其元素是从x位置开始的l个字符。时间复杂度: O(log N)。
- replace(int x,int l,crope r1):将第x个元素开头的l个元素替换为r1中的元素。时间复杂度: O(log N)。
- concatenate(+):使用’+’符号连接两条绳索。时间复杂度: O(1)。
以下是相同的程序:
程式2:
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():最长的绳索的尺寸保证可以表示。
以下是相同的程序:
程序3:
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