STL Classes – C++ (Part – 2)

STL Classes – C++ (Part – 2)

3 min read

In previous article we have learnt about Vector, Queue and stack. Now, we will learn about more STL classes which is so useful and essential.

Priority Queue STL

Priority queue will store element according to its priority. This type of queue is not properly follow FIFO principal.

Initialize :- priority_queue<data type> priorityqueue_name;

  1. push(value) :- push(value) checks priority then compare with other element and insert accordingly.
  2. pop() :- pop() is used to delete element from last of queue and reduced the size by one.
  3. front() :- front() returns value of first index of queue.
  4. back() :- back() returns value of last index of queue.
  5. size() :- size() returns size of queue.
  6. swap() :- swap() exchange elements of two queue.
#include<iostream>
#include<queue>
using namespace std;
int main()
{
  priority_queue<int> q1;
  q1.push(10);
  q1.push(1);
  q1.push(34);
  while (!q1.empty()) {
    cout<<q1.top()<<" "; //output :- 34 10 1
    q1.pop();
  }
  cout<<q1.front(); //output :- 34
  cout<<q1.back(); //output :- 1
  return 0;
}

List STL

List STL class is used to implement linked list. It is dynamical sized linked list. We can implement every type of linked list using this.

initialize :- list<datatype> list_name;

  1. front() :- front() returns the value of the first element in the list.
  2. back() :- back() returns the value of the last element in the list.
  3. push_front(value) :- push_front(value) inserts a new value at the beginning of the list .
  4. push_back(value) :- push_back(value) inserts a new value at the end of the list.
  5. pop_front() :- pop_front() delete the first element of the list, and reduces size of the list by one.
  6. pop_back() – pop_back() delete the last element of the list, and reduces size of the list by one.
  7. insert(itr, old_val, new_val) :- insert(itr, old_val, new_val) is used to insert the elements at any position of list.
#include<iostream>
#include<list>
using namespace std;

int main(){
  list<int> l1;
  l1.push_back(230);
  l1.push_back(780);
  l1.push_back(900);
  l1.push_back(120);
  while(!l1.empty()){
    cout<<l1.front()<<" ";
    l1.pop_front();
  }
  
  l1.pop_front(); //removing element from Front
  l1.pop_back(); //removing element from back
  
  return 0;
}

Pair STL

Pair STL is used to make pair of two different value with same or different data type. In this, Element are stored as pair of key and value where key should be unique and named as “first” but value can be duplicate and named as “second”. For accessing value we use its unique key.

initialize :- pair<data type 1, data type 2> pair_name

Library :- <utility>

#include <iostream> 
#include <utility> 
using namespace std; 
  
int main() 
{ 
    pair <int, string> p1 ; 
    p1.first = 20; 
    p1.second = 'ishvik' ; 
    cout << p1.first << " " ;  //output :- 20
    cout << p1.second << endl ; //output :- ishvik
    //another way of initializing 
    pair_name = make_pair("Hy this is simran ",09);
    return 0; 
} 
Choose your Reaction!
Leave a Comment