Make the most out of the C++ STL

Правка en2, от snowmanonahoe, 2023-12-12 00:50:27

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

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.

Теги c++, stl

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en2 Английский snowmanonahoe 2023-12-12 00:50:27 554 Add content based on comments. Thanks to AlphaMale06, Boboge, cosenza, drdilyor.
en1 Английский snowmanonahoe 2023-12-09 22:17:11 2077 Initial revision (published)