Hello everyone!
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.
Code#include "bits/stdc++.h"
using namespace std;
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
// initializing array
vector<int> a = {1, 2, 3, 4, 5, -6};
// checking if all elements are odds
bool allOdds = all_of(a.begin(), a.end(), [](int x) {
return x % 2 != 0;
});
if (allOdds) {
cout << "All are odds numbers\n";
} else {
cout << "All are not odds numbers\n";
}
// output:
// All are not odds numbers
}
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.
Code#include "bits/stdc++.h"
using namespace std;
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
// initializing array
vector<int> a = {1, 2, 3, 4, 5, -6};
// checking if any element is negative.
bool anyNegative = any_of(a.begin(), a.end(), [](int x) {
return x < 0;
});
if (anyNegative) {
cout << "There are exists a negative element\n";
} else {
cout << "All are positive elements\n";
}
// output:
// There are exists a negative element
}
none_of(): this function returns true if none of element satisfies the given condition, else returns false.
Code#include "bits/stdc++.h"
using namespace std;
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
// initializing array
vector<int> a = {1, 2, 3, 4, 5, -6};
// checking if no element is negative.
bool noNegative = none_of(a.begin(), a.end(), [](int x) {
return x < 0;
});
if (noNegative) {
cout << "No negative elements\n";
} else {
cout << "There are negative element\n";
}
// output:
// There are negative element
}
iota(): this function is used to assign continuous values to array.
Code#include "bits/stdc++.h"
using namespace std;
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
// initializing array
vector<int> a(6, 0);
// using iota() to assign values
iota(a.begin(), a.end(), 20);
// displaying the new array
cout << "The new array after assigning values is: ";
for (auto &x: a) {
cout << x << " \n"[&x == &a.back()];
}
// output:
// The new array after assigning values is: 20 21 22 23 24 25
}
accumulate(): this function returns the sum of all the values lying in a range between [first, last) with the variable sum.
Code#include "bits/stdc++.h"
using namespace std;
int fun(int x, int y) {
return x * y;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int initValue = 1;
vector<int> a = {5, 10, 15};
// simple default accumulate function
cout << "Result using accumulate: " << accumulate(a.begin(), a.end(), initValue) << "\n";
// using accumulate function with user-defined function
cout << "Result using accumulate with user-defined function: " << accumulate(a.begin(), a.end(), initValue, fun) << "\n";
// using accumulate function with pre-defined function
cout << "Result using accumulate with pre-defined function " << accumulate(a.begin(), a.end(), initValue, minus<int>()) << "\n";
// Output:
// Result using accumulate: 31
// Result using accumulate with user-defined function: 750
// Result using accumulate with pre-defined function -29
}
Thank you to all
Full text and comments »