Hi everyone!!! On this occasion I want to offer you a blog about the STL of C++11, a few weeks ago I was reading interesting information about this topic in GeeksforGeeks, and today I decided to share this information through this blog for all those interested. In advance, I want to thank all those who pay attention and interest to this blog, and thank any comments about it (positive or negative). Well, let's start now. I will start with some array algorithms in the C++11 STL, which I did not know before and that can have a very interesting use, depending on the problem. Array algorithms:
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 element one by on. It checks for a given property on every element and returns true when each element in range satisfies specified property, else returns false.
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// Initializing array
int arr[6] = {1, 2, 3, 4, 5, -6};
// Checking if all elements are odds.
bool ans = all_of(arr, arr + 6, [](int x) {return x & 1; } );
if (ans){
cout << "All are odds numbers\n";
}
else{
cout << "All are not odds numbers\n";
}
// Output:
// All are not odds numbers.
return 0;
}
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.
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// Initializing array
int arr[] = {1, 2, 3, 4, 5, -6};
// Checking if any element is negative.
bool ans = any_of(arr, arr + 6, [](int x) {return x < 0; } );
if (ans){
cout << "There are exists a negative element\n";
}
else{
cout << "All are positive elements\n";
}
// Output:
// There are exists a negative element
return 0;
}
none_of() : This function returns true if none of element satisfies the given condition else returns false.
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// Initializing array
int arr[] = {1, 2, 3, 4, 5, -6};
// Checking if no element is negative.
bool ans = none_of(arr, arr + 6, [](int x) {return x < 0; } );
if (ans){
cout << "No negative elements\n";
}
else{
cout << "There are negative element\n";
}
// Output:
// There are negative element
return 0;
}
copy_n() : copies one array elements to new arrays. 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.
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// Initializing array
int arr[] = {1, 2, 3, 4, 5, -6};
// Declaring second array
int ans[6];
copy_n(arr, 6, ans);
cout << "The new array after copying is: ";
for (int i = 0; i < 6; i++){
cout << ans[i] << " ";
}
cout << "\n";
// Output:
// The new array after copying is: 1 2 3 4 5 -6
return 0;
}
iota() : This function is used to assign continuous values to array. This function accepts 3 arguments, the array name, size, and starting number.
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// Initializing array
int arr[6] = {0};
// Using iota() to assign values
iota(arr, arr + 6, 20);
// Displaying the new array
cout << "The new array after assigning values is: ";
for (int i = 0; i < 6; i++){
cout << arr[i] << " ";
}
cout << "\n";
// Output:
// The new array after assigning values is: 20 21 22 23 24 25
return 0;
}
accumulate() : This function returns the sum of all the values lying in a range between [first, last) with the variable sum.
#include <bits/stdc++.h>
using namespace std;
int fun (int x, int y){
return x * y;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int sum = 1;
int arr[] = {5, 10, 15};
// Simple default accumulate function
cout << "Result using accumulate: ";
cout << accumulate(arr, arr + 3, sum) << "\n";
// Using accumulate function with user-defined function
cout << "Result using accumulate with user-defined function: ";
cout << accumulate(arr, arr + 3, sum, fun) << "\n";
// Using accumulate function with pre-defined function
cout << "Result using accumulate with pre-defined function ";
cout << accumulate(arr, arr + 3, sum, minus<int>()) << "\n";
// Output:
// Result using accumulate: 31
// Result using accumulate with user-defined function: 750
// Result using accumulate with pre-defined function -29
return 0;
}
Thank you all !!!