Many great functions offered by the C++ standard library go criminally underused. I've listed a few of my favorites here.
C++11
- std::iota fills a data structure with incrementing values.
C++17
- std::unique iterates through a sorted data structure and puts adjacent duplicate values at the end of the structure.
- std::count counts values in a data structure that compare equal to an argument.
- std::set_intersection finds the common elements in 2 sorted data structures.
- std::rotate shifts elements, placing the rightmost elements at the beginning as necessary.
C++20
- std::clamp takes 3 values and "clamps" the 2nd argument into the bounds set by the 1st and 3rd.
- std::partial_sum prefix sums a data structure.
- std::accumulate sums up the values of a data structure.
- std::adjacent_difference creates a difference array.
- <bit> has various functions for easier bitwise manipulation. Featuring std::popcount.
- std::midpoint averages 2 arguments, rounding towards the first.
C++23 (Not supported by Codeforces)
- std::unreachable marks unreachable code to enable compiler optimization and debug trapping. (Pre-C++23 alternatives: GCC/Clang:
__builtin_unreachable()
MSVC:__assume(false)
)
Miscellaneous
- std::max and std::min take an initializer list, so you don't have to nest them within themselves.
- Binary search on a monotonic function using std::partition_point and std::views::iota: See here
If I missed anything, comment it and I'll add it. I understand some of these functions are categorized in the wrong C++ version, but I can't figure out what the right one is for the ones that have been pointed out, such as partial_sum, accumulate, and adjacent_difference.
This is really cool. I'm a huge fan of STL, so thanks!
who cares ?
It's fun to know more stuff, and makes your code both shorter, cleaner and cooler to read and write.
At the same time this is just my opinion, but there's nothing wrong with sharing knowledge.
std::rotate is broken
Can you say how?
std::rotate(v.begin(), v.begin()+x, v.end()) will rotate the elements in the vector v by x to the left. It's just really convinient, could save you like 30 seconds of coding and maybe even more if you type slow.
For C++20:
Combine std::ranges::partition_point and std::views::iota to do the binary search for the answer.
e.g. 224876925
When I learned about lazy views factories, I spent hours trying to find a way to do this. Thanks!
partial_sum, accumulate and adjacent_difference weren't introduced in C++20
All these functions existed before c++20.
Is cppreference website preferred over cplusplus.com by most people ?
cppreference has more content. Some newer stuff aren't present in cplusplus.
I see, thanks.
cppreference.com is available as offline archive on IOI, if that matters
Oh, didn't know this. Nice.
Thanks :)
My favorite is
iota
. You can initialize the father array of dsu using only one line.iota(fa+1,fa+n+1,1);
std::clamp
description is wrong, it takes the value and then the 2 bounds of the range, and clamps the value to be inside the range.That's what I intended to say, I see now that 'range' could be misinterpreted as a pair of iterators. Will fix.
Auto comment: topic has been updated by snowmanonahoe (previous revision, new revision, compare).
std::merge / std::inplace_merge (linear-time merge procedure) are very convenient
Thank you for sharing! it's helpful for me.