
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Array algorithms in C++ STL
The C++ STL or Standard Template Library is a collection of general-purpose classes and functions with templates for implementing many algorithms and data structures such as vectors, lists, queues, and stacks. The 4 components of STL are: Containers, Algorithms, Iterators, and Functors.
In this article, we will discuss the functions of the <algorithm> header that work on arrays, and are introduced in C++ 11 and later versions.
C++ <algorithm> Header
The algorithm header provides various built-in functions that implement algorithms such as searching, sorting, finding the maximum element, etc.
These functions perform various operations on containers such as arrays, vectors, lists, etc. We can pass the first and last elements of a range of elements on which you need to perform the operation.
Here is a list of 9 C++ STL functions (C++ 11 onwards) that we are discussing in this article that can be used on the arrays:
- all_of() Function
- any_of() Function
- none_of() Function
- is_sorted() Function
- copy_n() Function
- minmax_element() Function
- move() Function
- shuffle() Function
- iota() Function
The all_of() Function
The all_of() function checks if all the elements in an array satisfy the given condition. It returns true if each element of the array satisfies the condition, it returns false if at least one element does not satisfy the condition. For example, we can check whether all the array elements are positive.
Syntax
The syntax of the all_of() function is given below:
template <class InputIterator, class UnaryPredicate> bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred);
Parameters
The parameters of the all_of() function is as follows:
- first: It represents the starting index.
- last: It represents the index up to which we want to check the condition.
- pred: A unary predicate function that accepts a single element and returns a boolean value.
Example
In this example, we are checking if all the elements of the given array are even using the all_of() function.
#include <iostream> #include <algorithm> using namespace std; int main() { int arr[] = {2, 4, 6, 8, 10}; int n = sizeof(arr) / sizeof(arr[0]); cout << "Given array is: "; for (int i = 0; i < 5; i++) { cout << arr[i] << " "; } cout << endl; if (all_of(arr, arr + n, [](int x) { return x % 2 == 0; })) { cout << "All elements are even"; } else { cout << "All elements are not even"; } }
The output of the above code is as follows:
Given array is: 2 4 6 8 10 All elements are even
The any_of() Function
The any_of() function is used to check if any element in the specified range of an array (or any other container) satisfies the given condition.
It returns true if at least one element satisfies the given condition, and it returns false if none of the elements satisfy the condition. For example, we can check if any element in an array is negative using this function.
Syntax
The syntax of the any_of() function is given below:
template <class InputIterator, class UnaryPredicate> bool any_of (InputIterator first, InputIterator last, UnaryPredicate pred);
Parameters
The parameters of the any_of() function is as follows:
- first: It represents the starting index.
- last: It represents the index up to which we want to check the condition.
- pred: A unary predicate function that accepts a single element and returns a boolean value.
Example
In this example, we are checking if any of the elements of the given array are negative using the any_of() function.
#include <iostream> #include <algorithm> using namespace std; int main() { int arr[] = {1, 3, -5, 7, 9}; int n = sizeof(arr) / sizeof(arr[0]); cout << "Given array is: "; for (int i = 0; i < 5; i++) { cout << arr[i] << " "; } cout << endl; if (any_of(arr, arr + n, [](int x) { return x < 0; })) { cout << "At least one element is negative"; } else { cout << "No element is negative"; } }
The output of the above code is as follows:
Given array is: 1 3 -5 7 9 At least one element is negative
The none_of() Function
The none_of() function is used for checking if none of the array elements satisfy the given condition. It returns true if no element satisfies the condition and returns false if at least one element satisfies the condition. For example, checking if no element is zero.
Syntax
The syntax of the none_of() function is given below:
template <class InputIterator, class UnaryPredicate> bool none_of (InputIterator first, InputIterator last, UnaryPredicate pred);
Parameters
The parameters of the none_of() function is as follows:
- first: It represents the starting index.
- last: It represents the index up to which we want to check the condition.
- pred: A unary predicate function that accepts a single element and returns a boolean value.
Example
In this example, we are checking if none of the elements of the given array is zero using the none_of() function.
#include <iostream> #include <algorithm> using namespace std; int main() { int arr[] = {1, 2, 3, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]); cout << "Given array is: "; for (int i = 0; i < 5; i++) { cout << arr[i] << " "; } cout << endl; if (none_of(arr, arr + n, [](int x) { return x == 0; })) { cout << "No element is zero"; } else { cout << "At least one element is zero"; } }
The output of the above code is as follows:
Given array is: 1 2 3 4 5 No element is zero
The is_sorted() Function
You can use the is_sorted() function to check if the specified range of array elements is sorted in ascending order. It returns true if sorted, and false if not sorted.
Syntax
The syntax of the is_sorted() function is given below:
template <class ForwardIterator> bool is_sorted (ForwardIterator first, ForwardIterator last);
Parameters
The parameters of the is_sorted() function is as follows:
- first: It represents the starting index.
- last: It represents the index up to which we want to check the condition.
Example
In this example, we are checking if the given array is sorted using the is_sorted() function.
#include <iostream> #include <algorithm> using namespace std; int main() { int arr[] = {1, 3, 5, 7, 9}; int n = sizeof(arr) / sizeof(arr[0]); cout << "Given array is: "; for (int i = 0; i < 5; i++) { cout << arr[i] << " "; } cout << endl; if (is_sorted(arr, arr + n)) { cout << "Array is sorted"; } else { cout << "Array is not sorted"; } }
The output of the above code is as follows:
Given array is: 1 3 5 7 9 Array is sorted
The copy_n() Function
To copy a specific number of elements from one array to another, we use the copy_n() function.
Syntax
The syntax of the copy_n() function is given below:
template <class InputIterator, class Size, class OutputIterator> OutputIterator copy_n (InputIterator first, Size n, OutputIterator result);
Parameters
The parameters of the copy_n() function is as follows:
- first: It represents the starting index of the source array.
- n: It represents the number of elements that we want to copy.
- result: It represents the starting index of the destination array.
Example
In this example, we are copying the first 3 elements of the src array to the dest array using the copy_n() function.
#include <iostream> #include <algorithm> using namespace std; int main() { int src[] = {10, 20, 30, 40, 50}; int dest[5] = {}; cout << "Source array: "; for (int i = 0; i < 5; i++) cout << src[i] << " "; cout << endl; copy_n(src, 3, dest); cout << "Destination array after copy: "; for (int i = 0; i < 5; i++) cout << dest[i] << " "; }
The output of the above code is as follows:
Source array: 10 20 30 40 50 Destination array after copy: 10 20 30 0 0
The minmax_element() Function
The minmax_element() function is used to return the smallest and the largest elements of the array in the specified range.
Syntax
The syntax of the minmax_element() function is given below:
template <class ForwardIterator> pair<ForwardIterator,ForwardIterator> minmax_element (ForwardIterator first, ForwardIterator last);
Parameters
The parameters of the minmax_element() function is as follows:
- first: It represent the starting array index.
- last: It represents the index up to which we want to search in the array.
Example
In this example, we have used the minmax_element() function to find the minimum and the maximum element of the array.
#include <iostream> #include <algorithm> using namespace std; int main() { int arr[] = {15, 8, 22, 4, 10}; int n = sizeof(arr) / sizeof(arr[0]); cout << "Given array is: "; for (int i = 0; i < 5; i++) { cout << arr[i] << " "; } cout << endl; auto result = minmax_element(arr, arr + n); cout << "Minimum element is: " << *result.first << endl; cout << "Maximum element is: " << *result.second; }
The output of the above code is as follows:
Given array is: 15 8 22 4 10 Minimum element is: 4 Maximum element is: 22
The move() Function
You can use the move() function to move the elements of one array (source) to another array (destination).
Syntax
The syntax of the move() function is given below:
template <class InputIterator, class OutputIterator> OutputIterator move (InputIterator first, InputIterator last, OutputIterator result);
Parameters
The parameters of the move() function are as follows:
- first: It represents the starting index of the source array.
- last: It represents the index up to which we want to move.
- result: It represents the starting index of the destination array.
Example
In this example, we are moving the elements of the source array to another array using the move() function.
#include <iostream> #include <algorithm> using namespace std; int main() { int src[] = {5, 10, 15, 20, 25}; int dest[5]; cout << "Source array before move: "; for (int i = 0; i < 5; i++) cout << src[i] << " "; cout << endl; move(src, src + 5, dest); cout << "Destination array after move: "; for (int i = 0; i < 5; i++) cout << dest[i] << " "; }
The output of the above code is as follows:
Source array before move: 5 10 15 20 25 Destination array after move: 5 10 15 20 25
The shuffle() Function
To randomly rearrange the array elements in the given range, you can use the shuffle() function. We use a random number generator g to randomly swap one element with another.
Syntax
The syntax of the shuffle() function is given below:
template <class RandomAccessIterator, class URNG> void shuffle (RandomAccessIterator first, RandomAccessIterator last, URNG&& g);
Parameters
The parameters of the shuffle() function are as follows:
- first: It represents the starting index.
- last: It represents the index up to which we want to shuffle.
- g: A random number generator function.
Example
In this example, we are randomly shuffling the elements of the array using the shuffle() function.
#include <iostream> #include <algorithm> #include <random> #include <ctime> using namespace std; int main() { int arr[] = {1, 2, 3, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]); cout << "Original array: "; for (int i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; shuffle(arr, arr + n, default_random_engine(time(0))); cout << "Shuffled array: "; for (int i = 0; i < n; i++) cout << arr[i] << " "; }
The output of the above code will vary due to random shuffling:
Original array: 1 2 3 4 5 Shuffled array: 3 1 5 2 4
The iota() Function
The iota() function is used when you want to fill an array with increasing values starting from a given value specified in its perimeter. It is a function of the <numeric> header.
Syntax
The syntax of the iota() function is given below:
template <class ForwardIterator, class T> void iota (ForwardIterator first, ForwardIterator last, T value);
Parameters
The parameters of the iota() function are as follows:
- first: It represents the starting index.
- last: It represents the index up to which we want to fill values.
- value: The initial value to start with.
Example
In this example, we are filling an array with increasing numbers starting from 1 using the iota() function.
#include <iostream> #include <numeric> using namespace std; int main() { int arr[5]; iota(arr, arr + 5, 1); cout << "Array after iota: "; for (int i = 0; i < 5; i++) cout << arr[i] << " "; }
The output of the above code is as follows:
Array after iota: 1 2 3 4 5
Complexity Comparison
Here is a comparison of the time and space complexity of all the above approaches.
<algorithm> Functions | Time Complexity | Space Complexity |
---|---|---|
all_of() Function | O(n) | O(1) |
any_of() Function | O(n) | O(1) |
none_of() Function | O(n) | O(1) |
is_sorted() Function | O(n) | O(1) |
copy_n() Function | O(n) | O(1) |
minmax_element() Function | O(n) | O(1) |
move() Function | O(n) | O(1) |
shuffle() Function | O(n) | O(1) |
iota() Function | O(n) | O(1) |