Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Блог пользователя snowmanonahoe

Автор snowmanonahoe, история, 10 месяцев назад, По-английски

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
  • Проголосовать: нравится
  • +75
  • Проголосовать: не нравится

»
10 месяцев назад, # |
  Проголосовать: нравится +13 Проголосовать: не нравится

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

  • »
    »
    10 месяцев назад, # ^ |
      Проголосовать: нравится -53 Проголосовать: не нравится

    who cares ?

    • »
      »
      »
      10 месяцев назад, # ^ |
      Rev. 2   Проголосовать: нравится +16 Проголосовать: не нравится

      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.

»
10 месяцев назад, # |
  Проголосовать: нравится +11 Проголосовать: не нравится

std::rotate is broken

  • »
    »
    10 месяцев назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Can you say how?

    • »
      »
      »
      10 месяцев назад, # ^ |
        Проголосовать: нравится +27 Проголосовать: не нравится

      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.

»
10 месяцев назад, # |
  Проголосовать: нравится +21 Проголосовать: не нравится

For C++20:

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

e.g. 224876925

  • »
    »
    10 месяцев назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    When I learned about lazy views factories, I spent hours trying to find a way to do this. Thanks!

»
10 месяцев назад, # |
  Проголосовать: нравится +6 Проголосовать: не нравится

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

»
10 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

»
10 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Thanks :)

»
10 месяцев назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

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

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

»
10 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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.

  • »
    »
    10 месяцев назад, # ^ |
      Проголосовать: нравится +1 Проголосовать: не нравится

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

»
10 месяцев назад, # |
  Проголосовать: нравится +9 Проголосовать: не нравится

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

»
10 месяцев назад, # |
  Проголосовать: нравится +9 Проголосовать: не нравится

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

»
10 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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