#include<iostream>#include<vector>#include<algorithm>usingnamespace std;
intmain(){
vector<int> vec1 {1,3,7,9,11,17,23};
all_of(vec1.begin(), vec1.end(), [](int x) { return (x &1) ==1;}) ? cout <<"All odds\n": cout <<"Not all odds\n";
vector<int> vec2 {1,3,6,8,9,11,13};
any_of(vec2.begin(), vec2.end(), [](int x) { return (x &1) ==0;}) ? cout <<"There are at least one even\n": cout <<"There are no any even\n";
none_of(vec1.begin(), vec1.end(), [](int x) { return (x &1) ==0;}) ? cout <<"There are no any even\n": cout <<"There are at least one even\n";
return0;
}
#include<iostream>#include<algorithm>usingnamespace std;
intmain(){
int arr[] = {1,2,3,4,5,6};
int arr2[6];
copy_n(arr, 6, arr2);
for (int i : arr2){
cout << i <<" ";
}
return0;
}
// C++ code to demonstrate working of iota()
#include<iostream>
#include<numeric> // for iota()
usingnamespace std;
intmain(){
// 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] <<" ";
return0;
}
This function returns an iterator pointing to the partition point of container i.e. the first element in the partitioned range [beg,end) for which condition is not true. The container should already be partitioned for this function to work.
// C++ code to demonstrate the working of
// stable_partition() and partition_point()
#include<iostream>
#include<algorithm> // for partition algorithm
#include<vector> // for vector
usingnamespace std;
intmain()
{
// Initializing vector
vector<int> vect = { 2, 1, 5, 6, 8, 7 };
// partitioning vector using stable_partition()
// in sorted order
stable_partition(vect.begin(), vect.end(), [](int x)
{
return x%2==0;
});
// Displaying partitioned Vector
cout <<"The partitioned vector is : ";
for (int&x : vect) cout << x <<" ";
cout << endl;
// Declaring iterator
vector<int>::iterator it1;
// using partition_point() to get ending position of partition
auto it = partition_point(vect.begin(), vect.end(), [](int x)
{
return x%2==0;
});
// Displaying partitioned Vector
cout <<"The vector elements returning true for condition are : ";
for ( it1= vect.begin(); it1!=it; it1++)
cout <<*it1 <<" ";
cout << endl;
return0;
}
This function copies the partitioned elements in the different containers mentioned in its arguments. It takes 5 arguments. Beginning and ending position of container, beginning position of new container where elements have to be copied (elements returning true for condition), beginning position of new container where other elements have to be copied (elements returning false for condition) and the condition. Resizing new containers is necessary for this function.
// C++ code to demonstrate the working of
// partition_copy()
#include<iostream>
#include<algorithm> // for partition algorithm
#include<vector> // for vector
usingnamespace std;
intmain()
{
// Initializing vector
vector<int> vect = { 2, 1, 5, 6, 8, 7 };
// Declaring vector1
vector<int> vect1;
// Declaring vector1
vector<int> vect2;
// Resizing vectors to suitable size using count_if() and resize()
int n = count_if (vect.begin(), vect.end(), [](int x)
{
return x%2==0;
} );
vect1.resize(n);
vect2.resize(vect.size()-n);
// Using partition_copy() to copy partitions
partition_copy(vect.begin(), vect.end(), vect1.begin(),
vect2.begin(), [](int x)
{
return x%2==0;
});
// Displaying partitioned Vector
cout <<"The elements that return true for condition are : ";
for (int&x : vect1)
cout << x <<" ";
cout << endl;
// Displaying partitioned Vector
cout <<"The elements that return false for condition are : ";
for (int&x : vect2)
cout << x <<" ";
cout << endl;
return0;
}
// C++ code to demonstrate the working of
// apply() and sum()
#include<iostream>
#include<valarray> // for valarray functions
usingnamespace std;
intmain()
{
// Initializing valarray
valarray<int> varr = { 10, 2, 20, 1, 30 };
// Declaring new valarray
valarray<int> varr1 ;
// Using apply() to increment all elements by 5
varr1 = varr.apply([](int x){return x=x+5;});
// Displaying new elements value
cout <<"The new valarray with manipulated values is : ";
for (int&x: varr1) cout << x <<" ";
cout << endl;
// Displaying sum of both old and new valarray
cout <<"The sum of old valarray is : ";
cout << varr.sum() << endl;
cout <<"The sum of new valarray is : ";
cout << varr1.sum() << endl;
return0;
}
// C++ code to demonstrate the working of
// max() and min()
#include<iostream>
#include<valarray> // for valarray functions
usingnamespace std;
intmain()
{
// Initializing valarray
valarray<int> varr = { 10, 2, 20, 1, 30 };
// Displaying largest element of valarray
cout <<"The largest element of valarray is : ";
cout << varr.max() << endl;
// Displaying smallest element of valarray
cout <<"The smallest element of valarray is : ";
cout << varr.min() << endl;
return0;
}
// C++ code to demonstrate the working of
// shift() and cshift()
#include<iostream>
#include<valarray> // for valarray functions
usingnamespace std;
intmain()
{
// Initializing valarray
valarray<int> varr = { 10, 2, 20, 1, 30 };
// Declaring new valarray
valarray<int> varr1;
// using shift() to shift elements to left
// shifts valarray by 2 position
varr1 = varr.shift(2);
// Displaying elements of valarray after shifting
cout <<"The new valarray after shifting is : ";
for ( int&x : varr1) cout << x <<" ";
cout << endl;
// using cshift() to circulary shift elements to right
// rotates valarray by 3 position
varr1 = varr.cshift(-3);
// Displaying elements of valarray after circular shifting
cout <<"The new valarray after circular shifting is : ";
for ( int&x : varr1) cout << x <<" ";
cout << endl;
return0;
}