Open In App

STL Ropes in C++

Ropes are scalable string implementation. They are designed for efficient operation that involves the string as a whole. Operations such as assignment, concatenation, and sub-string take time that is nearly independent of the length of the string.

A rope is a binary tree where each leaf (end node) holds a string and a length (also known as a “weight”), and each node further up the tree holds the sum of the lengths of all the leaves in its left subtree. A node with two children thus divides the whole string into two parts: the left sub-tree stores the first part of the string, the right subtree stores the second part of the string, and a node’s weight is the length of the first part.



For rope operations, the strings stored in nodes are assumed to be constant immutable objects in the typical non-destructive case, allowing for some copy-on-write behavior. Leaf nodes are usually implemented as basic fixed-length strings with a reference count attached for deallocation when no longer needed, although other garbage collection methods can be used as well.

Note: The rope class and rope header are SGI extensions; they are not part of the C++ standard library.



Declaration:
Ropes are defined in the same way as vectors as “vector<int>”. But for character ropes, we can use crope as “rope<char>”.

Below is the program for the same:

Program 1:




// C++ program to illustrate the use
// of ropes using Rope header file
#include <ext/rope>
#include <iostream>
 
// SGI extension
using namespace __gnu_cxx;
 
using namespace std;
 
// Driver Code
int main()
{
    // rope<char> r = "abcdef"
    crope r = "abcdef";
 
    cout << r << "\n";
 
    return 0;
}

 
 

Output: 
abcdef

 

 

Operations allowed on Rope:

 

 

Below is the program for the same:

 

Program 2:

 




// C++ program to illustrate the use
// of ropes using Rope header file
#include <ext/rope>
#include <iostream>
 
// SGI extension
using namespace __gnu_cxx;
using namespace std;
 
// Driver Code
int main()
{
    // rope<char> 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 position 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 popping"
         << " out 3 elements: " << r;
 
    return 0;
}

Output
Initial rope: geeksforgeeks
Rope after pushing f: geeksforgeeksgfg
Rope after inserting gfg at position 2: ggfgeeksforgeeksgfg
Rope after removing gfg inserted just before: geeksforgeeksgfg
Rope after replacing characters: g00ksforgeeksgfg
Subrope at position 2: g00ksforgeeksgfg
Final rope after popping out 3 elements: g00ksforgeeks

 
 

 

Capacity Functions:

 

 

Below is the program for the same:

 

Program 3:

 




// C++ program to illustrate the use
// of ropes using Rope header file
#include <ext/rope>
#include <iostream>
 
// SGI extension
using namespace __gnu_cxx;
using namespace std;
 
// Driver Code
int main()
{
    // rope<char> r = "abcdef"
    crope r = "abcdef";
 
    cout << r.size() << endl;
    cout << r.max_size() << endl;
    return 0;
}

 
 

Output: 
6
1836311902

 

 

Iterators:

 

 

Below is the program for the same:

 

Program 4:

 




// C++ program to illustrate the use
// of ropes using Rope header file
#include <ext/rope>
#include <iostream>
 
// SGI extension
using namespace __gnu_cxx;
using namespace std;
 
// Driver Code
int main()
{
    // rope<char> r = "abcdef"
    crope r = "abcdef";
 
    rope<char>::iterator it;
    for (it = r.mutable_begin();
         it != r.mutable_end(); it++) {
 
        // Print the value
        cout << char((*it) + 2)
             << "";
    }
 
    return 0;
}

 
 

Output: 
cdefgh

 

 


Article Tags :