snowmanonahoe's blog

By snowmanonahoe, history, 12 months 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?
»
12 months ago, # |
  Vote: I like it +13 Vote: I do not like it

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

  • »
    »
    12 months ago, # ^ |
      Vote: I like it -53 Vote: I do not like it

    who cares ?

    • »
      »
      »
      12 months ago, # ^ |
      Rev. 2   Vote: I like it +16 Vote: I do not like it

      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.

»
12 months ago, # |
  Vote: I like it +11 Vote: I do not like it

std::rotate is broken

  • »
    »
    12 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Can you say how?

    • »
      »
      »
      12 months ago, # ^ |
        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.

»
12 months ago, # |
  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

»
12 months ago, # |
  Vote: I like it +6 Vote: I do not like it

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

»
12 months ago, # |
  Vote: I like it 0 Vote: I do not like it

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

  • »
    »
    12 months ago, # ^ |
      Vote: I like it +32 Vote: I do not like it

    cppreference has more content. Some newer stuff aren't present in cplusplus.

  • »
    »
    12 months ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    cppreference.com is available as offline archive on IOI, if that matters

    • »
      »
      »
      12 months ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      Oh, didn't know this. Nice.

»
12 months ago, # |
  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);

»
12 months ago, # |
  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.

  • »
    »
    12 months ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    That's what I intended to say, I see now that 'range' could be misinterpreted as a pair of iterators. Will fix.

»
12 months ago, # |
  Vote: I like it +9 Vote: I do not like it

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

»
12 months ago, # |
  Vote: I like it +9 Vote: I do not like it

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

»
12 months ago, # |
  Vote: I like it 0 Vote: I do not like it

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