Open In App

Important functions of STL Components in C++

Improve
Improve
Like Article
Like
Save
Share
Report

C++




// C++ code
#include <iostream>
#include <utility>
using namespace std;
 
int main()
{
    // Declaring the PAIR1 of int and char
    // IF pair is not initialized then ,
    // default value of int/double is 0 and
    // for string/char it is NULL
    pair<int, char> PAIR1;
    cout << PAIR1.first << " ";
   
    // NULL value therefore, not displayed
    cout << PAIR1.second
         << endl;
 
    // Initializing the pair during it's Declaration
    pair<string, double> PAIR2("GeeksForGeeks", 1.23);
    cout << PAIR2.first << " ";
    cout << PAIR2.second << endl;
 
    pair<string, double> PAIR3;
     
    // Inserting Value in pair using make_pair function
    PAIR3 = make_pair("GeeksForGeeks is Best", 4.56);
    cout << PAIR3.first << " ";
    cout << PAIR3.second << endl;
 
    pair<int, int> PAIR4;
     
    // Inserting Value in pair using {}(curly brackets)
    PAIR4 = { 4, 8 };
    cout << PAIR4.first << " ";
    cout << PAIR4.second << endl;
 
    return 0;
}


 STL provides a range of data structures that are very useful in various scenarios. A lot of data structures are based on real-life applications. It is a library of container classes, algorithms, and iterators. It is a generalized library and so, its components are parameterized.

Most common Data Structures used are:

  1. Vector
  2. Stack
  3. Queue
  4. Priority queue
  5. Set
  6. List
  7. Ordered Maps
  8. Unordered Maps

Containers or container classes store objects and data. There are in total seven standards “first-class” container classes and three container adaptor classes and only seven header files that provide access to these containers or container adaptors.

Note: We can include just one library, i.e., #include <bits/stdc++.h> that includes all the STL libraries but in certain competitions, including this library can make the code slow. To overcome this problem, we can add specific libraries to access particular data structures of STL. Also while removing the elements, it is required to take care if the data structure is empty or not. Calling remove function on an empty data structure leads to error. Below are the some Data Structures with their illustration shown 

  1. Vector: The major problem while using arrays was that we had to specify size. This drawback was overcome by vectors. Vectors internally work as dynamically allocated arrays, which is the main reason as to how we can add elements without specifying the size of the vector. When the size of the vector becomes equal to capacity, the capacity of vector increases and thus we can add further elements.

Header file:

#include <vector>

Syntax: 

vector<data type> variable_name;

Most common function for vector: 

  1. push_back(): Used to push the element at the end of the vector. For faster method, use emplace_back().
  2. pop_back(): Used to remove the last element from the vector.
  3. size(): Returns the size of the vector.
  4. clear(): Deletes all the content of the vector.
  5. erase(): Deletes the specified index or data.
  6. empty(): Returns boolean value True if vector is empty, else returns False.
  7. Iterator lower_bound(Iterator first, Iterator last, const val): lower_bound returns an iterator pointing to the first element in the range [first, last) which has a value not less than ‘val’.
  8. Iterator upper_bound(Iterator first, Iterator last, const val): upper_bound returns an iterator pointing to the first element in the range [first, last) which has a value greater than ‘val’.

C++




// C++ program to illustrate the
// function of vector in C++
#include <iostream>
 
// Header file for vector if
// <bits/stdc++.h> not included
#include <vector>
using namespace std;
 
// Function to print the vector
void print(vector<int> vec)
{
 
    // vec.size() gives the size
    // of the vector
    for (int i = 0; i < vec.size(); i++) {
        cout << vec[i] << " ";
    }
 
    cout << endl;
}
 
// Driver Code
int main()
{
    // Defining a vector
    vector<int> vec;
 
    // Put all natural numbers
    // from 1 to 10 in vector
    for (int i = 1; i <= 10; i++) {
        vec.push_back(i);
    }
 
    cout << "Initial vector: ";
 
    // print the vector
    print(vec);
 
    // Size of vector
    cout << "Vector size: " << vec.size() << "\n";
 
    // Check of vector is empty
    if (vec.empty() == false)
        cout << "Is vector is"
             << " empty: False\n";
 
    // Popping out 10 form the vector
    vec.pop_back();
    cout << "Vector after popping: ";
    print(vec);
 
    // Deleting the first element
    // from the vector using erase()
    vec.erase(vec.begin());
    cout << "Vector after erase"
         << " first element: ";
    print(vec);
 
    // Clear the vector
    vec.clear();
    cout << "Vector after "
         << "clearing: None ";
    print(vec);
 
    // Check if vector is empty
    if (vec.empty() == true)
        cout << "Is vector is"
             << " empty: True\n";
}


Output

Initial vector: 1 2 3 4 5 6 7 8 9 10 
Vector size: 10
Is vector is empty: False
Vector after popping: 1 2 3 4 5 6 7 8 9 
Vector after erase first element: 2 3 4 5 6 7 8 9 
Vector after clearing: None 
Is vector is empty: True

2. Stack: It is Last In First Out (LIFO) data structure. It can be implemented using arrays, linked lists, and vectors. Some problems like reversing an element or string, parenthesis check, print next greater element, postfix expression, etc, can be done using stack class rather than making all the functions we can use its inbuilt functions.
Header file:

#include <stack>

Syntax: 

stack<data_type> variable_name;

Most common function for stack:  

  1. push(): Used to push the element at top of the stack.
  2. pop(): Deletes the top element of the stack but do not returns it.
  3. top(): Returns the top element of the stack.
  4. empty(): Return boolean value, ie, True if stack is empty, else returns false.
  5. size(): Returns the size of the stack.

C++




// C++ program to illustrate the
// function of stack in C++
#include <iostream>
 
// Header file for stack
#include <stack>
using namespace std;
 
// Function to print the stack
void print(stack<char> s)
{
 
    // Loops runs till stack
    // becomes empty
    while (s.empty() == false) {
 
        // Prints the top element
        cout << s.top() << " ";
 
        // Now pops the same top element
        s.pop();
    }
 
    cout << "\n";
}
 
// Driver Code
int main()
{
    // Given char array
    char array[]
        = { 'G', 'E', 'E', 'K',
            'S', 'F', 'O', 'R', 'G',
            'E', 'E', 'K', 'S' };
 
    // Defining stack
    stack<char> s;
 
    // Check if stack is empty
    if (s.empty() == true) {
        cout << "Stack is currently Empty"
            << "\n";
    }
    else {
        cout << "Stack is not empty"
            << "\n";
    }
 
    // Push elements in stack
    for (int i = sizeof(array) / sizeof(array[0]) - 1;
        i >= 0; i--) {
        s.push(array[i]);
    }
 
    // Size of stack
    cout << "Size of stack: "
        << s.size() << "\n";
 
    // Content of stack
    cout << "Stack initially: ";
    print(s);
 
    // Returning the top
    // element of the stack
    cout << "Top element: "
        << s.top() << "\n";
 
    // Popping the top
    // element in stack
    s.pop();
 
    cout << "Stack after 1"
        << "pop operation: ";
    print(s);
 
    // Now checking the top element
    cout << "Top element after popping: "
        << s.top() << "\n";
 
    // Size of stack
    cout << "Size of stack"
        << "after popping: "
        << s.size() << "\n";
 
    // Again checking if the
    // stack is empty
    if (s.empty() == true) {
        cout << "Stack is currently Empty"
            << "\n";
    }
    else {
        cout << "Stack is not empty"
            << "\n";
    }
    return 0;
}


Output

Stack is currently Empty
Size of stack: 13
Stack initially: G E E K S F O R G E E K S 
Top element: G
Stack after 1pop operation: E E K S F O R G E E K S 
Top element after popping: E
Size of stackafter popping: 12
Stack is not empty

3.Queue: It is First In First Out (FIFO) data structure.The reason why we require queues use a lot of practical application of first in first out and when data doesn’t need to be processed early. For example, in a queue for buying tickets for a show, the one who enters the queue first, get the ticket first. It can be implemented using arrays, linked lists, and vectors just like stacks. Some applications of queue include level-order traversal in trees and graphs, in resource sharing, etc. 
Header file: 

#include <queue>

Syntax: 

queue<Data Type> variable_name;

Most common function for Queue:

  1. push(): Used to push the element at back of the queue
  2. pop(): Deletes the front element of the queue but does not return it.
  3. front(): Returns the front element of the queue, or the element that is first in the line.
  4. empty(): Return boolean value, ie, True if queue is empty, else returns false
  5. back(): Returns the last element of queue.
  6. size(): Returns the size of the queue.

C++




// C++ program to illustrate the
// function of vector in C++
#include <iostream>
 
// Header file for queue
#include <queue>
using namespace std;
 
// Function to print the queue
void print(queue<char> q)
{
    for (int i = 0; i < q.size(); i++) {
 
        // Printing the front element
        cout << q.front() << " ";
 
        // Popping the front element
        q.pop();
    }
 
    cout << "\n";
}
 
// Driver Code
int main()
{
    // Given array
    char array[]
        = { 'G', 'E', 'E', 'K', 'S' };
 
    // Defining queue
    queue<char> q;
 
    if (q.empty() == true) {
        cout << "Queue is empty\n";
    }
 
    for (int i = 0; i < 5; i++) {
        q.push(array[i]);
    }
 
    cout << "Queue Initially: ";
    print(q);
 
    // Front element
    cout << "Front element: "
        << q.front() << "\n";
 
    // Back element
    cout << "Back Element: "
        << q.back() << "\n";
 
    // Size of queue
    cout << "Size of queue: "
        << q.size() << "\n";
 
    // Empty
    if (q.empty() == false) {
        cout << "Queue is not empty\n";
    }
    return 0;
}


Output

Queue is empty
Queue Initially: G E E 
Front element: G
Back Element: S
Size of queue: 5
Queue is not empty

4.Priority Queue: This data structure is similar to queues, but the order of first out is decided by a priority set by the user. The main functions include getting top priority element, insert, delete the top priority element or decrease the priority. Data structure Heaps is used and not BST, as in BST, creating trees is costly than heaps, and the complexity of heaps is better. Also, heaps provide Complete Binary Tree and Heap order property satisfying all the properties of Priority Queue. Priority Queue has 2 variations, Min Heap and Max Heap. 
Complex problems like finding k largest or smallest elements, merge k unsorted arrays, Dijkstra’s Shortest Path Algorithm, Huffman code for compression, Prim’s Algorithm, etc. can be implemented easily.
Header file: 

#include <queue>

Syntax:

Min Priority Queue: priority_queue<Data Type> variable_name; 
Max Priority Queue: priority_queue <Data Type, vector, greater> variable_name;

Most common function for priority queue: 

  1. push(): Used to push the element in the queue
  2. pop(): Deletes the top priority element of the queue but does not return it. Deletes element with max priority in max heap else deletes min element
  3. size(): Returns the size of the queue.
  4. empty(): Returns boolean value, ie, true if queue is empty, else return false.
  5. top(): Returns the top element of the queue. In max priority queue, it returns the maximum, while in min priority queue, it returns the minimum value.

C++




// C++ program to illustrate the
// function of priority queue in C++
#include <iostream>
 
// Header file for priority
// queue, both MIN and MAX
#include <queue>
using namespace std;
 
// Function to print the
// min priority queue
void print_min(
    priority_queue<int, vector<int>, greater<int> > q)
{
    while (q.empty() == false)
    {
        // Print the front
        // element(MINIMUM)
        cout << q.top() << " ";
 
        // Pop the minimum
        q.pop();
    }
    cout << "\n";
}
 
// Function to print the
// min priority queue
void print_max(priority_queue<int> q)
{
    while (q.empty() == false)
    {
        // Print the front
        // element(MAXIMUM)
        cout << q.top() << " ";
 
        // Pop the maximum
        q.pop();
    }
    cout << "\n";
}
 
// Driver Code
int main()
{
    // MIN priority queue
    priority_queue<int> max_pq;
 
    // MAX priority_queue
    priority_queue<int, vector<int>, greater<int> > min_pq;
 
    // Is queue empty
    if (min_pq.empty() == true)
        cout << "MIN priority "
             << "queue is empty\n";
 
    if (max_pq.empty() == true)
        cout << "MAX priority"
             << " queue is empty\n";
 
    cout << "\n";
 
    for (int i = 1; i <= 10; i++)
    {
        min_pq.push(i);
        max_pq.push(i);
    }
 
    cout << "MIN priority queue: ";
    print_min(min_pq);
 
    cout << "MAX priority queue: ";
    print_max(max_pq);
    cout << "\n";
 
    // Size
    cout << "Size of min pq: " << min_pq.size() << "\n";
    cout << "Size of max pq: " << max_pq.size() << "\n";
    cout << "\n";
 
    // Top element
    cout << "Top of min pq: " << min_pq.top() << "\n";
    cout << "Top of max pq: " << max_pq.top() << "\n";
    cout << "\n";
 
    // Pop the front element
    min_pq.pop();
    max_pq.pop();
 
    // Queus after popping
    cout << "MIN priority "
         << "queue after pop: ";
    print_min(min_pq);
    cout << "MAX priority "
         << "queue after pop: ";
    print_max(max_pq);
    cout << "\n";
 
    // Size after popping
    cout << "Size of min pq: " << min_pq.size() << "\n";
    cout << "Size of max pq: " << max_pq.size() << "\n";
    cout << "\n";
 
    // Is queue empty
    if (min_pq.empty() == false)
        cout << "MIN priority "
             << " queue is not empty\n";
 
    if (max_pq.empty() == false)
        cout << "MAX priority queue"
             << " is not empty\n";
}


Output

MIN priority queue is empty
MAX priority queue is empty

MIN priority queue: 1 2 3 4 5 6 7 8 9 10 
MAX priority queue: 10 9 8 7 6 5 4 3 2 1 

Size of min pq: 10
Size of max pq: 10

Top of min pq: 1
Top of max pq: 10

MIN priority queue after pop: 2 3 4 5 6 7 8 9 10 
MAX priority queue after pop: 9 8 7 6 5 4 3 2 1 

Size of min pq: 9
Size of max pq: 9

MIN priority  queue is not empty
MAX priority queue is not empty

4.Set: Sets are associative containers in which each element is unique. The elements cannot be modified once inserted in the set. A set ignores the duplicate values and all the elements are stored in sorted order. This data structure is particularly useful when incoming elements need to be sorted and modification is not required. 
The sets can store elements in two orders, increasing order or decreasing order. 
Header file:

#include <set> 

Syntax:

Increasing order: set<Data type> variable_name;
Decreasing order :set<Data type, greater<Data type> > variable_name; 

Most common function for Set: 

  1. insert(): This function is used to insert a new element in the Set.
  2. begin(): This function returns an iterator to the first element in the set.
  3. end(): It returns an iterator to the theoretical element that follows the last element in the set.
  4. size(): Returns the total size of the set.
  5. find(): It returns an iterator to the searched element if present. If not, it gives an iterator to the end.
  6. count(): Returns the count of occurrences in a set. 1 if present, else 0.
  7. empty(): Returns boolean value, ie, true if empty else false.

5.List: Lists stores data in non-contiguous manner.Elements of the list can be scattered in the different chunks of memory.Access to any particular index becomes costly as traversal from know index to that particular index has to be done, hence it is slow than vector.Zero-sized lists are also valid. 
Header file:

#include <list> 

Syntax: 

list<Data Type> variable_name;

Most common function for list:

  1. push_front(element): Inserts a new element ‘element’ at the beginning of the list .
  2. push_back(element) : Inserts a new element ‘element’ at the end of the list.
  3. pop_front(): Removes the first element of the list.
  4. pop_back(): Removes the last element of the list.
  5. front() : Returns the value of the first element in the list.
  6. back() : Returns the value of the last element in the list .
  7. empty(): Returns boolean value, ie, true if empty else false.

6.Unordered Maps: Suppose you have a pencil box and some pens. You put some pens in the box, that would be random and not ordered unlike ordered maps but you can access any pen and work with it. The same is unordered maps, the elements are stored randomly but you can access any order anytime. The main difference is between ordered maps and unordered maps are the way the compiler stores them. It has two arguments, the first is called KEY, and the other is called Value. The keymaps to the value and is always unique.

Header file: 

#include <unordered_map> 

Syntax: 

unordered_map<Data Type> variable name;

Most common function for unordered map: 

  1. count(): Returns boolean values, ie, 1 if the key passes exists, else false.
  2. erase(key) : Returns the passed key.
  3. clear() : Deletes the entire map.
  4. size() : Returns the size of the map.

7.Ordered Maps: Suppose you have a new shelf in your room and some books. You arrange the books one after another but after arranging any number of books, you can access arranged books in any order and keep the book in the same place when you’ve read it. This is an example of a map. You fill the values one after another in order and the order is maintained always, but you can access any element anytime. This data structure is also implemented using dynamic arrays. Like unordered maps, it has two arguments, the first is called KEY, and the other is called Value. The keymaps to the value and is always unique.

Header file:

#include <ordered_map>

Syntax: 

ordered_map<Data Type> variable name;

Most common function for ordered map:

  1. count() : Returns boolean values, ie, 1 if the key passes exists, else false.
  2. erase(key): Returns the passed key.
  3. clear(): Deletes the entire map.
  4. size(): Returns the size of the map.

Other Useful STL are – 

1.) Pair : The pair container is a simple container defined in <utility> header consisting of two data elements or objects. The first element is referenced as ‘first’ and the second element as ‘second’ and the order is fixed (first, second). Pair is used to combine together two values which may be different in type. Pair provides a way to store two heterogeneous objects as a single unit. Pair can be assigned, copied and compared. The array of objects allocated in a map or hash_map are of type ‘pair’ by default in which all the ‘first’ elements are unique keys associated with their ‘second’ value objects.To access the elements, we use variable name followed by dot operator followed by the keyword first or second.

Header File : 

#include <utility>

Syntax : 

pair (data_type1, data_type2) Pair_name;

Most common function for Pair :

  1. make_pair() : This template function allows to create a value pair without writing the types explicitly.

Below is  the code to implement the above function : –

C++




// C++ code
#include <iostream>
#include <utility>
using namespace std;
 
int main()
{
    // Declaring the PAIR1 of int and char
    // IF pair is not initialized then ,
    // default value of int/double is 0
    // and for string/char it is NULL
    pair<int, char> PAIR1;
    cout << PAIR1.first << " ";
   
    // NULL value therefore, not displayed
    cout << PAIR1.second
         << endl;
 
    // Initializing the pair during it's Declaration
    pair<string, double> PAIR2("GeeksForGeeks", 1.23);
    cout << PAIR2.first << " ";
    cout << PAIR2.second << endl;
 
    pair<string, double> PAIR3;
     
    // Inserting Value in pair using make_pair function
    PAIR3 = make_pair("GeeksForGeeks is Best", 4.56);
    cout << PAIR3.first << " ";
    cout << PAIR3.second << endl;
 
    pair<int, int> PAIR4;
     
    // Inserting Value in pair using {}(curly brackets)
    PAIR4 = { 4, 8 };
    cout << PAIR4.first << " ";
    cout << PAIR4.second << endl;
 
    return 0;
}


If you have an array of pair and you use inbuilt sort function then , by default it sort the array on the basis of first value(i.e. obj.first) of the array of each element  .To sort in Descending order pass the function according to which you want to sort the array. 

Below Code will show how to sort the Array of pair : –

C++




// C++ Code
#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
 
bool ascending_secondValue(pair<int, int> a,
                           pair<int, int> b)
{
    return a.second < b.second;
}
 
bool descending_firstValue(pair<int, int> a,
                           pair<int, int> b)
{
    return a.first > b.first;
}
 
bool descending_secondValue(pair<int, int> a,
                            pair<int, int> b)
{
    return a.second > b.second;
}
 
// Driver Code
int main()
{
 
    pair<int, int> PAIR1[5];
    PAIR1[0] = make_pair(1, 3);
    PAIR1[1] = make_pair(13, 4);
    PAIR1[2] = make_pair(5, 12);
    PAIR1[3] = make_pair(7, 9);
   
     // Using {} to insert element instead
     // of make_pair you can use any
    PAIR1[4]
        = { 11, 2 };
 
    cout << "Sorting array in Ascending  "
            "order on the basis of First value - "
         << endl;
    sort(PAIR1, PAIR1 + 5);
    for (int i = 0; i < 5; i++) {
        cout << PAIR1[i].first << "  " << PAIR1[i].second
             << endl;
    }
 
    pair<int, int> PAIR2[5];
    PAIR2[0] = make_pair(1, 3);
    PAIR2[1] = make_pair(13, 4);
    PAIR2[2] = make_pair(5, 12);
    PAIR2[3] = make_pair(7, 9);
    PAIR2[4] = make_pair(11, 2);
 
    cout << "Sorting array in Ascending  "
            " order on the basis of Second value - "
         << endl;
    sort(PAIR2, PAIR2 + 5, ascending_secondValue);
    for (int i = 0; i < 5; i++)
    {
        cout << PAIR2[i].first
              << "  " << PAIR2[i].second
             << endl;
    }
 
    pair<int, int> PAIR3[5];
    PAIR3[0] = make_pair(1, 3);
    PAIR3[1] = make_pair(13, 4);
    PAIR3[2] = make_pair(5, 12);
    PAIR3[3] = make_pair(7, 9);
    PAIR3[4] = make_pair(11, 2);
 
    cout << "Sorting array in Descending order on the "
            "basis of First value - "
         << endl;
    sort(PAIR3, PAIR3 + 5, descending_firstValue);
    for (int i = 0; i < 5; i++) {
        cout << PAIR3[i].first << "  "
             << PAIR3[i].second
             << endl;
    }
 
    pair<int, int> PAIR4[5];
    PAIR4[0] = make_pair(1, 3);
    PAIR4[1] = make_pair(13, 4);
    PAIR4[2] = make_pair(5, 12);
    PAIR4[3] = make_pair(7, 9);
    PAIR4[4] = make_pair(11, 2);
 
    cout << "Sorting array in Descending order on the "
            "basis of Second value - "
         << endl;
    sort(PAIR4, PAIR4 + 5, descending_secondValue);
    for (int i = 0; i < 5; i++) {
        cout << PAIR4[i].first << "  "
             << PAIR4[i].second
             << endl;
    }
 
    return 0;
}


Ordered Set : Ordered set is a policy based data structure in g++ that keeps the unique elements in sorted order. It performs all the operations as performed by the set data structure in STL in log(n) complexity and performs two additional operations also in log(n) complexity .

  1. order_of_key (k) : Number of items strictly smaller than k .
  2. find_by_order(k) : K-th element in a set (counting from zero).

Header File and namespace :- 

#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

The necessary structure required for the ordered set implementation is :

tree < int ,  null_type ,  less ,  rb_tree_tag ,  tree_order_statistics_node_update >

You can read about them in detail here .

Additional functions in the ordered set other than the set are – 

  1. find_by_order(k): It returns to an iterator to the kth element (counting from zero) in the set in O(log n) time.To find the first element k must be zero.

          Let us assume we have a set s : {1, 5, 6, 17, 88}, then :

          *(s.find_by_order(2)) : 3rd element in the set i.e. 6

         *(s.find_by_order(4)) : 5th element in the set i.e. 88 

      2. order_of_key(k) : It returns to the number of items that are strictly smaller than our item k in O(log n) time.

          Let us assume we have a set s : {1, 5, 6, 17, 88}, then :

          s.order_of_key(6) : Count of elements strictly smaller than 6 is 2.

          s.order_of_key(25) : Count of elements strictly smaller than 25 is 4. 

NOTE :  ordered_set is used here as a macro given to 
tree<int, null_type, less, rb_tree_tag, tree_order_statistics_node_update>.
Therefore it can be given any name as macro other than ordered_set
but generally in the world of competitive programming it is commonly referred as ordered set
as it is a set with additional operations.

C++




// C++ program
#include <iostream>
using namespace std;
 
// Header files, namespaces,
// macros as defined above
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
 
// ordered_set is just macro you can give any
// other name also
#define ordered_set                              \
    tree<int, null_type, less<int>, rb_tree_tag, \
         tree_order_statistics_node_update>
 
// Driver program to test above functions
int main()
{
    // Ordered set declared with name o_set
    ordered_set o_set;
 
    // insert function to insert in
    // ordered set same as SET STL
    o_set.insert(5);
    o_set.insert(1);
    o_set.insert(2);
 
    // Finding the second smallest element
    // in the set using * because
    // find_by_order returns an iterator
    cout << *(o_set.find_by_order(1)) << endl;
 
    // Finding the number of elements
    // strictly less than k=4
    cout << o_set.order_of_key(4) << endl;
 
    // Finding the count of elements less
    // than or equal to 4 i.e. strictly less
    // than 5 if integers are present
    cout << o_set.order_of_key(5) << endl;
 
    // Deleting 2 from the set if it exists
    if (o_set.find(2) != o_set.end()) {
        o_set.erase(o_set.find(2));
    }
 
    // Now after deleting 2 from the set
    // Finding the second smallest element in the set
    cout << *(o_set.find_by_order(1)) << endl;
 
    // Finding the number of
    // elements strictly less than k=4
    cout << o_set.order_of_key(4) << endl;
 
    return 0;
}


Output

2
2
2
5
1


Last Updated : 02 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads