With pllk introducing 100 more problems to the CSES problemset, the race for shortest code is on. Here in this blog, I talk about the many different ways of shortening code to get that shortest code.
std::any_of
This is a very useful function that returns true iff any of the elements in the range [first, last)
satisfies some condition. Let's say you want to figure out if an array contains at least one 9. You could just write the naive loop and waste time in contest.
bool ok = 0;
for(int i = 0; i < (int)(a).size(); ++i) {
if(a[i] == 9) {
ok = 1;
break;
}
}
but you could be smart and write this all in one line.
bool ok = any_of(a.begin(), a.end(), [](int x) { return x == 9; });
Time Complexity
This is linear aka $$$O(n)$$$.
std::all_of
This is another useful function that functions similar to std::any_of
. The difference is that it returns true iff all the elements in the range [first, last)
follow some condition.
Let's say you want to find if the array consists of all 9's.
bool ok = 1;
for(int i = 0; i < (int)(a).size(); ++i) {
if(a[i] != 9) {
ok = 0;
}
}
Now this method saves a couple of lines which means that you'll get the fastest submit time. Guaranteed or I will give you your money back. :D
bool ok = all_of(a.begin(), a.end(), [](int x) { return x == 9; });
Time Complexity
Like the function that I mentioned, this is also $$$O(n)$$$.
std::none_of
This is yet another function that is close relatives of the two mentioned above. This function returns true iff all the elements does not follow some condition.
Let's say you want to find if the array doesn't contain 9.
Noobs will do:
bool ok = 1;
for(int i = 0; i < (int)(a).size(); ++i) {
if(a[i] == 9) {
ok = 0;
}
}
Pros would do:
bool ok = none_of(a.begin(), a.end(), [](int x) { return x == 9; });
This next one is very useful and it appears quite frequently in problems on various judges.
Time Complexity
Linear, $$$O(n)$$$.
std::for_each
Thanks to Ta180m for pointing this out in the comments. This function applies a function hld to all of the elements in the range of [first, last)
.
Lets say that you want to increase all the elements in some vector by 5.
Naive way:
for(int i = 0; i < (int)(a).size(); ++i) {
a[i] += 5;
}
A shorter way to write it would be:
auto change = [&](int& x) {
x += 5;
};
for_each(a.begin(), a.end(), change);
As mentionted by purplesyringa, if you want to iterate from begin ... end
it is better to use range-based for loops. std::for_each
is only really useful when your iterating from something else other than begin ... end
.
Time Complexity
$$$O(n \cdot X)$$$ where $$$X$$$ is the complexity of applying the function hld once.
std::count
This functions counts the number of elements in the range [first, last)
that are equal to some variable val.
Noobinho:
int cnt = 0;
for(int i = 0; i < (int)(a).size(); ++i) {
cnt += (a[i] == x);
}
Proinho:
int cnt = count(a.begin(), a.end(), x);
Time Complexity
$$$O(n)$$$.
std::find
This function returns the first iterator that compares equal to val.
Thanks to _rey for pointing this out to me. :)
NOTE: if using std::find
on sets, use set::find
instead. This guarantees it to be $$$O(\log n)$$$ where $$$n$$$ is the size of the set.
Nubs:
int hld = -1;
for(int i = 0; i < (int)(a).size(); ++i) {
if(a[i] == x) {
hld = i;
break;
}
}
Prubs:
int hld = find(a.begin(), a.end(), x) - a.begin();
Time Complexity
Linear, $$$O(n)$$$.
std::accumulate
Many coders use this function but I am still going to include it just in case you don't know the uses of it. This function returns the sum of init and all the elements from [first, last)
.
Let's say that you want to find the sum of all the elements in the vector. Beginners would do:
int sum = 0;
for(int i = 0; i < (int)(a).size(); ++i) {
sum += a[i];
}
Now, we can use std::accumulate
to do this in 1 line.
Thanks for N8LnR2Nqf8Q4FN for pointing this out to me.
NOTE: Be mindful for overflow when using std::accumulate
. If you know that the sum will overflow, use 0LL
otherwise use 0
. If your not sure using 0LL
won't hurt that much anyways.
int sum = accumulate(all(a), 0LL);
Time Complexity
$$$O(n)$$$
std::binary_search
This function is pretty useful in finding if the element val appears in the sorted range [first, last)
.
Let's say that you want to find if the element val appears in the sorted array. Standard binary search looks something like this.
bool ok = 0;
int l = 0, r = (int)(a).size(), target = val;
while(l < r) {
int mid = (l + r) / 2;
if(a[mid] < target) {
l = mid + 1;
} else if(a[mid] == target) {
ok = 1;
break;
} else {
r = mid;
}
}
bool ok = binary_search(a.begin(), a.end(), val);
Time Complexity
$$$O(\log n)$$$
std::max_element
This function returns the max element in the range [first, last)
.
int cur = -INF;
for(int i = 0; i < (int)(a).size(); ++i) {
cur = max(cur, a[i]);
}
Better way to do it is:
int cur = *max_element(a.begin(), a.end());
Time Complexity
$$$O(n)$$$
std::min_element
This is basically the same as std::max_element
but returns the min element in the range [first, last)
.
int cur = INF;
for(int i = 0; i < (int)(a).size(); ++i) {
cur = min(cur, a[i]);
}
int cur = *min_element(a.begin(), a.end());
Time Complexity
Linear in time, $$$O(n)$$$.
std::is_sorted
This function is for checking if the range [first, last)
is sorted. Its pretty self-explanatory.
bool ok = 1;
for(int i = 1; i < (int)(a).begin(); ++i) {
if(a[i] < a[i - 1]) {
ok = 0;
}
}
bool ok = is_sorted(a.begin(), a.end());
Alternatively, if you want to check if the range is sorted but in reverse order, you can first reverse the sequence and then check if it's sorted.
reverse(a.begin(), a.end());
bool ok = is_sorted(a.begin(), a.end());
Time Complexity
$$$O(n)$$$
std::lexicographical_compare
This function is used in many problems that compares two containers and checks which one is lexicographically first.
bool ok = lexigraphical_compare(a.begin(), a.end(), b.begin(), b.end());
Time Complexity
$$$O(n)$$$.
std::partial_sum
Thanks to Kavaliro for pointing this out to me. Imagine doing a prefix sum problem. You want to obviously compute the prefix sums. Naive way to do it would be
for(int i = 1; i < (int)(a).size(); ++i) {
a[i] += a[i - 1];
}
Now, as Kavaliro pointed out, this could be done in 1 line.
partial_sum(a.begin(), a.end(), a.begin());
Suffix sums are much the same thing
partial_sum(a.rbegin(), a.rend(), a.rbegin());
Time Complexity
Both are linear, $$$O(n)$$$.
std::adjacent_difference
Thanks to elsantodel90 for pointing this out to me. Since I already included the std function for prefix sum, it "only natural to mention adjacent_difference too" — elsantode190. So here it is.
vector<int> hld = a;
for(int i = 1; i < si(hld); ++i) {
hld[i] -= a[i - 1];
}
This takes 4 lines and much more characters than this:
adjacent_difference(a.begin(), a.end(), a.begin());
Saves a good amount of characters and shorter to type out.
Time Complexity
$$$O(n)$$$
std::iota
Thanks to purplesyringa for pointing this out to me! This function assigns every value from [first, last)
successive values of val
where val
is the argument to the function. A useful scenario is when you have a DSU and you want to initialize it by setting each node's parent to itself. Now this can be done naively
for(int i = 0; i < n; ++i) {
par[i] = i;
}
or it can be done in a much shorter way
iota(par.begin(), par.end(), 0);
Time Complexity
$$$O(n)$$$
Thats all for now, as I have school work to do. But I'll continue to update this blog with other useful functions. :D
Nice blog!
std::for_each
is also very useful.Thanks for the suggestion!
EDIT: Added the function. A bit late I think lol.
Well done!
#define all(x) (x).begin(), (x).end()
will save you some more time. However, most people don't spend as much time coding the solution as they do thinking about it, so I am not sure whether such shortcuts are necessaryI didn't want to have macros in my code as many people don't like macros when reading code. As for your second point I should've made it clear in the blog.
Edit: Made it clear in the introduction what the purpose of this blog is for.
std::accumulate
is another useful one.Noted.
Another one .. function to count numbers greater than or equal to x
int count_x = count_if(v.begin(), v.end(), [](int a) { return (a >= x); });
Noted. Thanks!
Time complexity of all function mentioned above is O(n).
Noted.
Here is a good talk from cppcon about STL algorithms like these: https://www.youtube.com/watch?v=2olsGf6JIkU
Mah boi has a positive contribution now! Alo nice blog.. Good job!
You can get all of these useful functions here --> https://en.cppreference.com/w/cpp/header/algorithm
Sure I personally prefer http://cplusplus.com/reference/algorithm/
Just be careful with functions like
std::find
with sets.std::find
is a linear time function, not log time. If you want to find an element in a set in log time, useset::find
. Cplusplus referenceSo, instead of
use
Also, both
std::find
andset::find
return an iterator, and you can't subtract set iterators, soxs.find(x) - xs.begin()
won't compile if xs is a set.Thanks for the tip! I will add that note to
std::find
.EDIT: Added note.
Or better use
count
to check containment in set.please correct me if I'm wrong somewhere
the complexity of
count()
is $$$O(count + \log n)$$$ (not $$$O(\log n)$$$), andcount()
uses more iterators thanfind()
, so I still preferset::find() != set::end()
, although in CP, the difference won't be that muchWell, set doesn't contain duplicates, so it will be log(n).
count()
will either return 0 or 1. See this for reference https://en.cppreference.com/w/cpp/container/set/countYou are mentioning
set::count
, which is different fromstd::count
. std::count will be linear in a set, whileset::count
will be log time.Thanks for this blog. Please keep adding.
Would be great to have similar blogs for other languages
I would like to say a bit about
std::accumulate
if you have, say,
std::accumulate(begin(C), end(C), x)
, then the accumulation part will have the declared type ofx
, so ifx
isint
for example, and the sum is too large, this might cause overflow, so make surex
has the right type firstThanks for the tip!
std::accumulate
can not only accumulate sum tointi
, but also accumulate values over any binary operation.For example:
accumulate(all(a), 1, multiplies<int>())
will return the product of every element inall(a)
.In general:
accumulate(all(a), init, myfun)
Using multiplies() is probably a very bad idea because it will most likely overflow but yeah, using a function, perhaps a lambda
[](const int& a, const int& b){return (a*b) % MOD;}
would be nice.woah just use kotlin for these shenanigans
if only they allow me to use Kotlin at onsite competitions
Kotlin and is_sorted???
(1 until a.size).all { a[it - 1] <= a[it] }
or even
a == a.sorted()
if you don't care about asymptotics.I don't get it
You run over all indices of the array
a
except 0 and check whether for each element (a[i]
) the previous one (a[i - 1]
) is less or the same.I can understand your comment without context, but what the point of your post with?
Guranteed or I will give you your money back. :D
Thanks <3
You missed a very handy function
partial_sum
. it allows you to make prefixes and suffixes quicklyOh nice! I will update the blog when I have time. Thanks for the tip!
It is only natural to mention adjacent_difference too:
https://en.cppreference.com/w/cpp/algorithm/adjacent_difference
Note that it is implemented in such a way that it is the exact inverse of partial_sum
This is difference array? Nice!
UPDATE: Edited blog to add
std::adjacent_difference
Thanks for the tip!Very good reference! I think you could add the time complexity of each function :)
Thanks for the feedback, will add time complexity.
UPDATE: Added time complexities for all functions
partial_sum could be used for a lot more, for instance, to create a suffix max.
(But I wish I can use the stl max function with shorter code)
Of course you can
Since your lambda just max function itself.
I don't think that works because max has multiple definitions, I don't believe the compiler can deduce the template type on its own.
That's why it's $$$max<int>$$$ instead of $$$max$$$
Example
OHH, it doesn't work if you include <bits/stdc++.h> !
Got mistake(
Due to multiple overloads it must be:
And it still be incorrect with C++20 because taking address to std function there has unspecified behaviour.
I haven't seen any of these functions until I read this blog... Is anybody here the same?
By the way, there is also a useful function in STL called
std::nth_element
. This function is mainly used to arrange the k-th smallest integer in the array element and place it in the array, which can be called at any time. Could poster consider adding this function?P.S. How to use it in your code:
Time Complexity: $$$\mathcal{O}(n)$$$.
std::iota
is useful too. I often use it for sorting and DSU initialization.Compare:
vs
DSU:
vs
Added, thanks for the suggestion!
BTW, it's probably worth mentioning that range-based loop is often better than
for_each
. Compare:vs
for_each
is still useful when you use bounds other thanbegin..end
though.in std::for_each what purpose does this [&] serve I didn't understand.
You can read more about lambda functions here https://docs.microsoft.com/en-us/cpp/cpp/lambda-expressions-in-cpp?view=msvc-160
Thank you for the list, it really helped to open my mind to using more functional programming in c++. I rewrote some of my code to be a decent amount shorter with these. One function I used you didn't mention is
std::count_if
, a generalization of count for when you want to check how many elements of an array satisfy a condition.