C++ Algorithm Library

C++ Programming Examples - Algorithm Library

The C++ algorithm library includes a huge number of functions that are designed to work with a large number of elements at once or a range of elements. STL algorithm library functions are used for various purposes such as searching, sorting, counting, manipulating, merging, etc. C++ Algorithm library provides execution policy classes and objects for the standard library algorithms that support a variety of execution policies.

The following section contains various C++ programs on STL algorithm functions on modifying and non-modifying sequence operations, sorting, partitioning, binary search, heap, merge, permutation, min, and max operations. Each sample program includes a program description, C++ code, and program output. All examples have been compiled and tested on Windows and Linux systems.

Here is the listing of C++ programming examples on Algorithm Library:

  1. C++ Algorithms on Modifying Sequence Operations
  2. C++ Algorithms on Non-modifying Sequence Operations
  3. C++ Algorithms on Sorting Operations
  4. C++ Algorithms on Partitioning Operations
  5. C++ Algorithms on Binary Search Operations
  6. C++ Algorithms on Heap Operations
  7. C++ Algorithms on Merge Operations
  8. C++ Algorithms on Min/Max Operations
  9. C++ Algorithms on Permutation Operations
  10. C++ Algorithms on Other Functions

1. C++ Algorithms on Modifying Sequence Operations

Program Description
transform() in C++ C++ Program to Illustrate the transform() Function
replace() Function in C++ C++ Program to Demonstrate the replace() Function
replace_if() in C++ C++ Program to Demonstrate the replace_if() Function
fill() Function in C++ C++ Program to Demonstrate the fill() Function
fill_n() in C++ C++ Program to Demonstrate the fill_n() Function
generate() in C++ C++ Program to Demonstrate the generate() Function
generate_n() in C++ C++ Program to Demonstrate the generate_n() Function
remove() Function in C++ C++ Program to Demonstrate the remove() Function
remove_if() in C++ C++ Program to Demonstrate the remove_if() Function
unique() in C++ C++ Program to Remove Equal Adjacent Elements Using unique() Function
unique_copy() in C++ C++ Program to Demonstrate the unique_copy() Function
reverse() in C++ C++ Program to Reverse the Order Of Elements Using reverse() Function
reverse_copy() in C++ C++ Program to Demonstrate the reverse_copy() Function
rotate() Function in C++ C++ Program to Demonstrate the rotate() Function
rotate_copy() in C++ C++ Program to Demonstrate the rotate_copy() Function
random_shuffle() in C++ C++ Program to Demonstrate the random_shuffle() Function
Swap Values using swap() in C++ C++ Program to Swap Values Using swap() Function
swap() Function in C++ C++ Program to Swap Elements of Containers Using swap() Function
swap_ranges() in C++ C++ Program to Demonstrate the swap_ranges() Function
iter_swap() in C++ C++ Program to Swap Elements Using iter_swap() Function

advertisement

2. C++ Algorithms on Non-modifying Sequence Operations

Program Description
Map Elements using for_each in C++ C++ Program to Print Map using for_each algorithm
Vector Elements using for_each() in C++ C++ Program to Print Vector Elements Using for_each() Algorithm
Vector Addition using for_each() in C++ C++ Program to Compute Sum of Elements of Vector using for_each() Function
mismatch() in C++ C++ Program to Demonstrate mismatch() Function
find() Function in C++ C++ Program to Demonstrate find() Function
find_if() in C++ C++ Program to Demonstrate find_if() on string vector
find_first_of() in C++ C++ Program to Demonstrate the find_first_of() Function
adjacent_find() in C++ C++ Program to Demonstrate the adjacent_find() Function
count_if() in C++ C++ Program to Demonstrate count_if() Function

3. C++ Algorithms on Sorting Operations

Program Description
sort() Function in C++ C++ Program to Sort Container Elements Using sort() Function
stable_sort() in C++ C++ Program to Demonstrate the stable_sort() Function
partial_sort_copy() in C++ C++ Program to Demonstrate the partial_sort_copy() Function
nth_element() in C++ C++ Program to Demonstrate nth_element() Function

4. C++ Algorithms on Partitioning Operations

Program Description
partition() Algorithm in C++ C++ Program to Rearrange Container Elements using partition() Algorithm
stable_partition() in C++ C++ Program to Rearrange Container Elements using stable_partition() Algorithm

5. C++ Algorithms on Binary Search Operations

Program Description
lower_bound() in C++ C++ Program to Determine First Occurrence of An Element using lower_bound() Algorithm
upper_bound() in C++ C++ Program to Determine Last Occurrence of An Element using upper_bound() Algorithm

6. C++ Algorithms on Heap Operations

Program Description
push_heap() in C++ C++ Program to Push Elements to Heap using push_heap() Operation
pop_heap() in C++ C++ Program to Pop Element from Heap using pop_heap() Operation
make_heap() in C++ C++ Program to Create Heap using make_heap() Operation
sort_heap() in C++ C++ Program to Sort the Heap using sort_heap() Operation

7. C++ Algorithms on Merge Operations

Program Description
merge() in C++ C++ Program to Merge Two Sequences Using merge() Function
inplace_merge() in C++ C++ Program to Merge Two Sequences Using inplace_merge() Function

advertisement

8. C++ Algorithms on Min/Max Operations

Program Description
min() Function in C++ C++ Program to Print Minimum Value using min() Algorithm
min_element() in C++ C++ Program to Demonstrate the min_element() Algorithm on a Container
max() Function in C++ C++ Program to Print Maximum Value using max() Algorithm
max_element() in C++ C++ Program to Demonstrate the max_element() Algorithm on a Container

9. C++ Algorithms on Permutation Operations

Program Description
next_permutation() in C++ C++ Program to Demonstrate the next_permutation() Function
prev_permutation() in C++ C++ Program to Demonstrate the prev_permutation() Function

10. C++ Algorithms on Other Functions

Program Description
Splice Operation in C++ C++ Program to Demonstrate Splice Operation on Lists
size() and resize() in C++ C++ Program to Demonstrate size() and resize() Functions on Vector
Bidirectional Iterators in C++ C++ Program to Demonstrate Bidirectional Iterator on Vector Container
std::greater in C++ C++ Program to Illustrate the use of Greater Predicate
Helper Functions in C++ C++ Program to Illustrate use of Helper Functions
erase() Function in C++ C++ Program to Demonstrate erase() Function on Vectors
lexicographical_compare() in C++ C++ Program to Demonstrate the lexicographical_compare() Function

advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.