snowmanonahoe's blog

By snowmanonahoe, history, 2 years ago, In English

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.

Tags c++, stl
  • Vote: I like it
  • +75
  • Vote: I do not like it

| Write comment?
»
2 years ago, hide # |
 
Vote: I like it +13 Vote: I do not like it

This is really cool. I'm a huge fan of STL, so thanks!

»
2 years ago, hide # |
 
Vote: I like it +11 Vote: I do not like it

std::rotate is broken

  • »
    »
    2 years ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Can you say how?

    • »
      »
      »
      2 years ago, hide # ^ |
       
      Vote: I like it +27 Vote: I do not like it

      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.

»
2 years ago, hide # |
 
Vote: I like it +21 Vote: I do not like it

For C++20:

Combine std::ranges::partition_point and std::views::iota to do the binary search for the answer.

e.g. 224876925

»
2 years ago, hide # |
 
Vote: I like it +6 Vote: I do not like it

partial_sum, accumulate and adjacent_difference weren't introduced in C++20

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Is cppreference website preferred over cplusplus.com by most people ?

»
2 years ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

My favorite is iota. You can initialize the father array of dsu using only one line.

iota(fa+1,fa+n+1,1);

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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.

»
2 years ago, hide # |
 
Vote: I like it +9 Vote: I do not like it

Auto comment: topic has been updated by snowmanonahoe (previous revision, new revision, compare).

»
2 years ago, hide # |
 
Vote: I like it +9 Vote: I do not like it

std::merge / std::inplace_merge (linear-time merge procedure) are very convenient

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Thank you for sharing! it's helpful for me.