Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

C++ STL: Algorithms Library

Divya Jain
Divya Jain

The C++ algorithms library is a vast collection of functions. It provides us with various inbuilt functions on searching, sorting, counting, manipulating, etc. The algorithms library operates on a range of elements and the range are defined as [first, last) where the last element is not included in the range. The algorithms library is present in #include<algorithm> header file. 

Algorithms operate directly on values through iterators and do not change the structure of the container. Thus, we can use the algorithms on any type of container. Also, this library saves our time and effort and provides us with the most reliable implementation of the algorithm.

Types of Algorithms in Algorithm Library

  • Modifying algorithms
  • Non-modifying algorithms
  • Sorting algorithms
  • Searching algorithms
  • Maximum and minimum operations

Modifying algorithms

Some examples of modifying algorithms are:

  1. copy(first1, last1, first2): It copies the elements from the range first1 to last1 excluding last1 into the range starting from first2.
  2. fill(first, last, value): It assigns the given value to all the elements in the range.
  3. swap(container1, container2): It swaps the elements of one container with another.
  4. reverse(first, last): It reverses the order of elements in the given range.
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

void print(vector <int> & v) {
  for (int i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
  }
  cout << '\n';
}
int main() {
  vector <int> v1, v2;
  for (int i = 0; i < 6; i++) {
    v1.push_back(i + 1);
    v2.push_back(i * 10);
  }
  cout << "Initially the vector1 is ";
  print(v1);
  cout << "Initially the vector2 is ";
  print(v2);
  copy(v2.begin(), v2.end(), v1.begin());
  cout << "After copy operation, the vector1 is ";
  print(v1);
  fill(v1.begin(), v1.end(), 5);
  cout << "After fill operation, the vector1 is ";
  print(v1);
  swap(v1, v2);
  cout << "After swapping, the vectors are\n" << "vector1 is: ";
  print(v1);
  cout << "vector2 is: ";
  print(v2);
  reverse(v1.begin(), v1.end());
  cout << "After reverse operation, the vector1 is ";
  print(v1);
}

Output

Initially the vector1 is 1 2 3 4 5 6 
Initially the vector2 is 0 10 20 30 40 50 
After copy operation, the vector1 is 0 10 20 30 40 50 
After fill operation, the vector1 is 5 5 5 5 5 5 
After swapping, the vectors are
vector1 is: 0 10 20 30 40 50 
vector2 is: 5 5 5 5 5 5 
After reverse operation, the vector1 is 50 40 30 20 10 0 

Non Modifying algorithms

Some examples of non-modifying algorithms are:

  1. count(first, last, value): It returns the number of occurrences of the given value in the given range.
  2. equal(first1, last1, first2): It compares the value in the given range and returns true if the values are equal otherwise false.
  3. search(first1, last1, first2, last2): It swaps the elements of one container with another.
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

void print(vector <int> & v) {
  for (int i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
  }
  cout << '\n';
}
int main() {
  vector <int> v1, v2;
  for (int i = 0; i < 6; i++) {
    v1.push_back(i + 1);
    v2.push_back(i * 10);
  }
  v1.push_back(1);
  v1.push_back(1);
  cout << "Initially the vector1 is ";
  print(v1);
  cout << "Initially the vector2 is ";
  print(v2);
  int c = count(v1.begin(), v1.end(), 1);
  cout << "Number of occurrences of 1 in vector 1: " << c << '\n';
  cout << "Is vector1 equal to vector2, 0 represents false, 1 represents true: ";
  cout << equal(v1.begin(), v1.end(), v2.begin() + 2) << '\n';
  vector < int > ::iterator i;
  i = search(v1.begin(), v1.end(), v2.begin(), v2.end());
  //i now points to the end of v1
}

Output

Initially the vector1 is 1 2 3 4 5 6 1 1 
Initially the vector2 is 0 10 20 30 40 50 
Number of occurrences of 1 in vector 1: 3
Is vector1 equal to vector2, 0 represents false, 1 represents true: 0

Sorting algorithms

Some examples of sorting algorithms are:

  1. sort(start_iterator, end_iterator): It sorts the elements of the container in the given range.
  2. partial_sort(start, middle, end): It sorts the elements partially in the range (start, end) such that elements from start to middle are sorted in ascending order and are smallest in the container,
  3. is_sorted(start_iterator, end_iterator): It returns true if the elements in the given range are sorted.
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

void print(vector <int> & v) {
  for (int i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
  }
  cout << '\n';
}
int main() {
  vector <int> v = {9, 8, 7, 6, 5, 4, 3, 2, 1};;

  cout << "Initially the vector is ";
  print(v);
  partial_sort(v.begin(), v.begin() + 4, v.begin() + 9);
  cout << "After partial sorting, the vector is ";
  print(v);
  sort(v.begin(), v.end());
  cout << "After sorting, the vector is ";
  print(v);
  cout << "Is the vector sorted now, 1 represents true, 0 represents false: " << is_sorted(v.begin(), v.end());
}

Output

Initially the vector is 9 8 7 6 5 4 3 2 1 
After partial sorting, the vector is 1 2 3 4 9 8 7 6 5 
After sorting, the vector is 1 2 3 4 5 6 7 8 9 
Is the vector sorted now, 1 represents true, 0 represents false: 1

Searching algorithms

Some examples of searching algorithms are:

  1. binary_search(first, last, value): It searches for the value in the given range and returns true if it is found.
  2. equal_range(first, last, value): It returns a subrange of elements where the elements are equal to the given value.
  3. upper_bound(start_iterator, end_iterator, value): It returns the upper bound of the value in the given sorted range.
  4. lower_bound(start_iterator, end_iterator, value): It returns the lower bound of the value in the given sorted range.
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

void print(vector <int> & v) {
  for (int i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
  }
  cout << '\n';
}
int main() {
  vector <int> v = {9, 8, 7, 6, 5, 4, 3, 2, 1};;

  cout << "Initially the vector is ";
  print(v);
  partial_sort(v.begin(), v.begin() + 4, v.begin() + 9);
  cout << "After partial sorting, the vector is ";
  print(v);
  sort(v.begin(), v.end());
  cout << "After sorting, the vector is ";
  print(v);
  cout << "Is the vector sorted now, 1 represents true, 0 represents false: " << is_sorted(v.begin(), v.end());
}

Output

Initially the vector is 9 8 7 6 5 4 3 2 1 
After partial sorting, the vector is 1 2 3 4 9 8 7 6 5 
After sorting, the vector is 1 2 3 4 5 6 7 8 9 
Is the vector sorted now, 1 represents true, 0 represents false: 1

Maximum and Minimum operations

Some functions performing minimum and maximum operations are:

  1. min(value1, value2): It returns the minimum of the two values.
  2. max(value1, value2): It returns the maximum of the two values.
  3. min_element(start_iterator, end_iterator): It returns the minimum element in the given range.
  4. max_element(start_iterator, end_iterator): It returns the maximum element in the given range.
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

void print(vector <int> & v) {
  for (int i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
  }
  cout << '\n';
}
int main() {
  vector <int> v = {9, 8, 7, 6, 5, 4, 3, 2, 1};

  cout << "Initially the vector is ";
  print(v);
  sort(v.begin(), v.end());
  cout << "Is element 7 present in the vector, 1 represents true, 0 represents false: ";
  cout << binary_search(v.begin(), v.end(), 7) << '\n';
  pair < vector <int> ::iterator, vector <int> ::iterator > sub_range;
  sub_range = equal_range(v.begin(), v.end(), 2);
  vector <int> ::iterator it1, it2;
  it1 = upper_bound(v.begin(), v.end(), 6);
  cout << "Upper bound of 6 is " << * it1 << '\n';
  it2 = lower_bound(v.begin(), v.end(), 9);
  cout << "Lower bound of 9 is " << * it2 << '\n';
}

Output

Initially the vector is 9 8 7 6 5 4 3 2 1 
Is element 7 present in the vector, 1 represents true, 0 represents false: 1
Upper bound of 6 is 7
Lower bound of 9 is 9
Divya Jain
Divya Jain
Divya is an incoming SDE at Atlassian. She loves solving problems and web development. She loves to explore new things and is up for interesting conversations about tech.
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet