Please read the new rule regarding the restriction on the use of AI tools. ×

Make the most out of the C++ STL

Revision en1, by snowmanonahoe, 2023-12-09 22:17:11

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.

C++20

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.

If I missed anything, comment it and I'll add it.

Tags c++, stl

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English snowmanonahoe 2023-12-12 00:50:27 554 Add content based on comments. Thanks to AlphaMale06, Boboge, cosenza, drdilyor.
en1 English snowmanonahoe 2023-12-09 22:17:11 2077 Initial revision (published)