Array algorithms in C++ STL (all_of, any_of, none_of, copy_n and iota)
Last Updated :
25 Oct, 2022
From C++11 onwards, some new and interesting algorithms are added in STL of C++. These algorithms operate on an array and are useful in saving time during coding and hence useful in competitive programming as well.
all_of() This function operates on whole range of array elements and can save time to run a loop to check each elements one by one. It checks for a given property on every element and returns true when each element in range satisfies specified property, else returns false.
CPP
// C++ code to demonstrate working of all_of()
#include<iostream>
#include<algorithm> // for all_of()
using namespace std;
int main()
{
// Initializing array
int ar[6] = {1, 2, 3, 4, 5, -6};
// Checking if all elements are positive
all_of(ar, ar+6, [](int x) { return x>0; })?
cout << "All are positive elements" :
cout << "All are not positive elements";
return 0;
}
Output:
All are not positive elements
Time Complexity: O(n)
Auxiliary Space: O(1)
In the above code, -6 being a negative element negates the condition and returns false.
any_of() This function checks for a given range if there's even one element satisfying a given property mentioned in function. Returns true if at least one element satisfies the property else returns false.
CPP
// C++ code to demonstrate working of any_of()
#include<iostream>
#include<algorithm> // for any_of()
using namespace std;
int main()
{
// Initializing array
int ar[6] = {1, 2, 3, 4, 5, -6};
// Checking if any element is negative
any_of(ar, ar+6, [](int x){ return x<0; })?
cout << "There exists a negative element" :
cout << "All are positive elements";
return 0;
}
Output:
There exists a negative element
Time Complexity: O(n)
Auxiliary Space: O(1)
In above code, -6 makes the condition positive.
none_of() This function returns true if none of elements satisfies the given condition else returns false.
CPP
// C++ code to demonstrate working of none_of()
#include<iostream>
#include<algorithm> // for none_of()
using namespace std;
int main()
{
// Initializing array
int ar[6] = {1, 2, 3, 4, 5, 6};
// Checking if no element is negative
none_of(ar, ar+6, [](int x){ return x<0; })?
cout << "No negative elements" :
cout << "There are negative elements";
return 0;
}
Output:
No negative elements
Time Complexity: O(n)
Auxiliary Space: O(1)
Since all elements are positive, the function returns true.
copy_n() copy_n() copies one array elements to new array. This type of copy creates a deep copy of array. This function takes 3 arguments, source array name, size of array and the target array name.
CPP
// C++ code to demonstrate working of copy_n()
#include<iostream>
#include<algorithm> // for copy_n()
using namespace std;
int main()
{
// Initializing array
int ar[6] = {1, 2, 3, 4, 5, 6};
// Declaring second array
int ar1[6];
// Using copy_n() to copy contents
copy_n(ar, 6, ar1);
// Displaying the copied array
cout << "The new array after copying is : ";
for (int i=0; i<6 ; i++)
cout << ar1[i] << " ";
return 0;
}
Output:
The new array after copying is : 1 2 3 4 5 6
Time Complexity: O(n)
Auxiliary Space: O(n)
In the above code, the elements of ar are copied in ar1 using copy_n()
iota() This function is used to assign continuous values to array. This function accepts 3 arguments, the array name, size, and the starting number.
CPP
// C++ code to demonstrate working of iota()
#include<iostream>
#include<numeric> // for iota()
using namespace std;
int main()
{
// Initializing array with 0 values
int ar[6] = {0};
// Using iota() to assign values
iota(ar, ar+6, 20);
// Displaying the new array
cout << "The new array after assigning values is : ";
for (int i=0; i<6 ; i++)
cout << ar[i] << " ";
return 0;
}
Output:
The new array after assigning values is : 20 21 22 23 24 25
Time Complexity: O(n)
Auxiliary Space: O(1)
In the above code, continuous values are assigned to array using iota().
Similar Reads
C++ STL Algorithm Library Standard Template Library (STL) offers a rich collection of algorithms designed to operate on STL containers and beyond. It provides commonly used algorithms such as sorting, searching, copying, etc. These well tested algorithms are optimized for performance and provide a way to write cleaner, faste
3 min read
sort() in C++ STL In C++, sort() is a built-in function used to sort the given range in desired order. It provides a simple and efficient way to sort the data in C++, but it only works on data structures that provide random access to its elements such as vectors and arrays.Let's take a look at an example:C++#include
4 min read
Type Inference in C++ (auto and decltype) Type Inference is a feature in C++, using which the compiler automatically deduces the data type of an expression, function, or variable. Type inference was introduced with C++11 through the use of the auto and decltype.Before C++ 11, each data type had to be explicitly declared, which limited the v
5 min read
transform() in C++ STL In C++, transform() is a built-in STL function used to apply the given operation to a range of elements and store the result in another range. Letâs take a look at a simple example that shows the how to use this function:C++#include <bits/stdc++.h> using namespace std; int main() { vector<i
4 min read
Variadic function templates in C++ Variadic templates are class or function templates, that can take any variable(zero or more) number of arguments. In C++, templates can have a fixed number of parameters only that have to be specified at the time of declaration. However, variadic templates help to overcome this issue. Douglas Gregor
3 min read
Template Specialization in C++ Template in C++is a feature. We write code once and use it for any data type including user defined data types. For example, sort() can be written and used to sort any data type items. A class stack can be created that can be used as a stack of any data type. What if we want a different code for a p
5 min read
Merge operations using STL in C++ | merge(), includes(), set_union(), set_intersection(), set_difference(), ., inplace_merge, Some of the merge operation classes are provided in C++ STL under the header file "algorithm", which facilitates several merge operations in a easy manner. Some of them are mentioned below. merge(beg1, end1, beg2, end2, beg3) :- This function merges two sorted containers and stores in new container
7 min read
std::partition in C++ STL C++ has a class in its STL algorithms library which allows us easy partition algorithms using certain inbuilt functions. Partition refers to act of dividing elements of containers depending upon a given condition. Partition operations :1. partition(beg, end, condition) :- This function is used to pa
5 min read
accumulate() and partial_sum() in C++ STL accumulate() and partial_sum() functions are used to find the sum or any other accumulated value that is obtained by doing the addition or any other binary operation on the elements in the given range. Both of these functions are the part of STL Numeric Library and defined inside <numeric> hea
4 min read
numeric header in C++ STL | Set 2 (adjacent_difference(), inner_product() and iota()) The numeric header is part of the numeric library in C++ STL. This library consists of basic mathematical functions and types, as well as optimized numeric arrays and support for random number generation. Some of the functions in the numeric header: iotaaccumulatereduceinner_productpartial_sum etc.
4 min read