Getting Started with STL

Vipul Patel
Spider R&D
Published in
6 min readJan 18, 2022

C++ is a multi-purpose programming language used widely across the world. There is no doubt that even after being a very old language, it is one of the most efficient programming languages.

C++ provides a good range of built-in libraries. STL is an acronym for standard template library. It helps in making the software development faster and allows the user to do more with less.

STL has four components:

· Containers

· Iterators

· Algorithms

· Functions

Containers are used to manage collections of certain kinds of objects. There are many types of containers, but in this article, we are going to cover only these basic containers-

  1. Vector
  2. Queue
  3. Stack
  4. Dequeue

These containers use iterators for operation so before starting in detail about container we must have to learn about iterator.

An iterator is an object(similar to a pointer) that points to an element inside the container. We can use iterators to move through the contents of the container.

Basic Operations of iterators :-

1. begin() :- The member function begin() is used to return the first position of element in the container.

2. end() :- The member function end() is used to return the after end position of element in the container.

3. advance() :- The member function advance() is used to increment the iterator position till the specified number mentioned in its arguments. We can use ‘++’ operator to increment single position of iterator.

4. rbegin():- The member function rbegin() returns reverse iterator to reverse beginning. It moves from last to first element.

5. rend():- The member function rend() returns reverse iterator to reverse end.

Vector:- Vectors are similar to dynamic arrays. (Internally, vectors use a dynamically allocated array to store their elements).It can resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container.

Syntax- vector< object_type > vector_name;

The basic operations used in vector:-

size()- Returns the number of element present in vector. Time complexity of this operation is O(1). Syntax- vector_name.size();

  1. push_back()- It push the elements at back of vector. Time complexity of this operation is O(1). Syntax- vector_name.push_back(element);
  2. pop_back()- It removes the last elements of vector. Time complexity of this operation is O(1). Syntax- vector_name.pop_back(element);
  3. insert() -It inserts new elements before the specified position. Time complexity of this operation is O(1). Syntax- vector_name.insert(position,element);
  4. erase() -It removes the elements from the specified position or range. Time complexity of this operation is O(n).Syntax- vector_name.earse(position);
  5. swap()-It swaps the contents of one vector with another vector of same type(Sizes may differ). Time complexity of this operation is O(1)( It swaps the addresses of two vectors rather than swapping each element one by one). Syntax- swap(vector_name1,vector_name2);
  6. empty() -Check whether the container is empty. Time complexity of this operation is O(1).Syntax- vector_name.empty();

Output of above code:-

Size : 5
Vector v: 10 1 2 3 4
Is vector v is empty? No
Vector v1: 1 2 3
Vector v2: 4 5 6
After Swap:
Vector v1: 4 5 6
Vector v2: 1 2 3

Queues:- Queues are a type of container adaptor, which designed to operate in a first in first out (FIFO) context. In queue elements are inserted into one end of the container and extracted from the other.

Syntax- queue< object_type > queue_name;

Operations of Queue:-

  1. empty()- Check whether the queue is empty.Time complexity of this operation is O(1). Syntax-queue_name.empty();
  2. size()- Returns the number of elements in queue.Time complexity of this operation is O(1). Syntax-queue_name.size();
  3. push()- This function adds element at end of queue.Time complexity of this operation is O(1). Syntax-queue_name.push(element);
  4. pop()- This function delete element first element of queue.Time complexity of this operation is O(1). Syntax-queue_name.pop();
  5. front() Returns a reference to the first element of the queue. Time complexity of this operation is O(1). Syntax-queue_name.front();
  6. back() Returns a reference to the last element of the queue.Time complexity of this operation is O(1). Syntax-queue_name.back();
  7. swap()-It swaps the contents of one queue with another queue but the queues must be of same type, although sizes may differ.Time complexity of this operation is O(1). Syntax-swap(queue_name1,queue_name2);

Output of above code:-

The queue is : 1 2 3 4 5
queue.size() : 5
queue.front() : 1
queue.back() : 5
queue.pop() : 2 3 4 5
Is Queue is empty? yes

Stack:- Stacks are a type of container adaptor, which designed to operate in a Last in first out (LIFO) context. In stack elements are inserted into one end(top) of the container and extracted from the same end.

Syntax- stack< object_type > stack_name;

Operations of Stack:-

  1. empty()- Check whether the stack is empty.Time complexity of this operation is O(1). Syntax- stack_name.empty();
  2. size()- Returns the number of elements in stack.Time complexity of this operation is O(1). Syntax- stack_name.size();
  3. push()- This function adds element at top of stack.Time complexity of this operation is O(1). Syntax- stack_name.push(element);
  4. pop()- This function delete element top most element of stack.Time complexity of this operation is O(1). Syntax- stack_name.pop();
  5. top()- Returns a reference to the top most element of the stack. Time complexity of this operation is O(1). Syntax-stack_name.top();
  6. swap()-It swaps the contents of one stack with another stack but the stacks must be of same type, although sizes may differ.Time complexity of this operation is O(1). Syntax- swap(stack_name1,stack_name2);

Output of above Code:-

The stack is : 5 4 3 2 1
stack.size() : 5
stack.top() : 5
stack.pop() : 4 3 2 1
Is stack is empty? yes

Deque (Double Ended Queue) is sequence containers which is generalized version of Queue. It allows to insert and delete from both ends. It support both stack and queue operation.

Syntax:- deque<int> deque_name;

Deque

Operations of Deque:-

  1. insert()- Inserts an element and returns an iterator that points to the first of the newly inserted elements. Syntax:- deque.insert(iterator, element);
  2. push_front()-This function is used to push elements into a deque from the front. Syntax:- deque.push_front(element);
  3. push_back() -This function is used to push elements into a deque from the back. Syntax:- deque.push_back(element);
  4. pop_front()- This function is used remove elements from a deque from the front. Syntax:- deque.pop_front();
  5. pop_back()-This function is used to remove elements from a deque from the back. Syntax:- deque.pop_back();
  6. front()- This function is used to reference the first element of the deque container. Syntax:- deque.front();
  7. back()- This function is used to reference the last element of the deque container. Syntax:- deque.back();
  8. erase()- This function is used to remove elements from a container from the specified position or range. Syntax:- deque.erase(position1,position2);
  9. empty()- This function is used to check if the deque container is empty or not. Syntax:- deque.empty();
  10. size()- This function is used to return number of elements in the deque container. Syntax:- deque.size();

All above operations have time complexity O(1).

Output of above code:-

The Deque is : 5 4 3 2 1 6
Deque.size() : 6
Deque.front() : 5
Deque.back() : 6
Deque.pop_back() & insert 10 : 6 1 2 3 4 5 10
Is stack is empty? yes

Algorithm:-

The header algorithm defines a collection of predefined functions is designed to be used on ranges of elements of containers. Many operations like sorting can be performed by the functions.

Sorting- sort() function is used to sort elements array or vector in ascending order.

Syntax- sort(start_position,end_position);

Time Complexity- O(N log N).

Now question is how can we sort in descending order?

We can sort in any order by passing third parameter to specify the order of sorting. For descending order :-

Syntax- sort(start_position,end_position, greater<object_type>());

Output of above code:-

Array:
1 10 11 2 7 9 2 2
Array after sorting Ascending Order :
1 2 2 2 7 9 10 11
Array after sorting Descending order Order :
11 10 9 7 2 2 2 1

Conclusion
STL is very helpful to make your code short and reusable. It is very helpful for competitive programming. You can code fast using STL. It is also extensible, letting you add your own containers and algorithms.

Learning to use STL is not a small task. We can’t cover whole topic in one article. Stay connected with us for more on STL.

--

--