CppCon 2016: Marshall Clow “STL Algorithms - why you should use them, and how to write your own"

Поделиться
HTML-код
  • Опубликовано: 23 ноя 2024

Комментарии • 35

  • @emanuellandeholm5657
    @emanuellandeholm5657 Год назад +3

    I just found all these CppCon videos. Not a C++ dev by any means, but these lectures are so good. Thanks!

  • @YourCRTube
    @YourCRTube 8 лет назад +33

    Nice talk, but a bit too 'entry level'. May be follow-up next year?

    • @michaelchadwick4920
      @michaelchadwick4920 8 лет назад +13

      I agree. It's a good introduction, but not very satisfying for anyone who's already written quite a few STL-style algorithms.

  • @FalcoGer
    @FalcoGer 2 месяца назад +1

    @15:11 funny. that is in the STL now in c++23 as std::ranges::views::adjacent with an alias of std::ranges::views::pairwise for adjacent. It doesn't call a functor but rather returns a range of tuples, which you can then put into std::foreach for example. there is also std::ranges::views::slide which returns a range of ranges rather than a range of tuples.
    here is my take on all_pairs @ 21:55
    template
    auto all_pairs(R&& range)
    {
    const auto SIZE = static_cast(std::ranges::distance(range));
    // generate all unique index pairs
    auto idxPairs = std::views::cartesian_product(std::views::iota(0, SIZE), std::views::iota(0, SIZE))
    | std::views::filter(
    [](const auto& idxPair)
    {
    const auto& [i, j] = idxPair;
    return i != j;
    }
    );
    return std::views::transform(
    idxPairs,
    [&range](const auto& idxPair)
    {
    const auto& [i, j] = idxPair;
    return std::make_tuple(range[static_cast(i)], range[static_cast(j)]);
    }
    );
    }
    if you wanted to only use ascending indexes in the pairs, adjust the filter predicate to return i < j;

  • @__hannibaalbarca__
    @__hannibaalbarca__ 2 года назад +3

    I see video two times
    1. Just to understand English
    2. To Understand Algorithm.

  • @videofountain
    @videofountain 8 лет назад +6

    Practical Gentle Introduction.

  • @OlliS71
    @OlliS71 2 месяца назад

    a f( first, first[-1] ) could be more efficient. But I guess the compiler does this optimization by itself, but its not guaranteed.

  • @benyuan620
    @benyuan620 7 лет назад +14

    but compile error is really not so friendly

  • @mrlithium69
    @mrlithium69 7 лет назад +9

    I wish he explained 12:00 why you would NOT want to use post-increment first++ operators instead of ++first , he even says the compile down to the same thing. What is the big deal about post-increment operators in the STD/STL

    • @daggawagga
      @daggawagga 7 лет назад +18

      He mentions the reason some time later. Using the post-increment creates another requirement imposed on the iterator. If you have less requirements then the algorithm can be applied in a wider variety of cases. Another case mentioned later uses the same reasoning when using first != last instead of first < last. All iterator types support !=, but not all of them support

    • @stephenhowe4107
      @stephenhowe4107 4 года назад +9

      They are inefficient. The canonical way to implement them is to save the old iterator value, call ++iterator, return the old value.
      I would only call the post-increment iterator if I needed the old value and do wish to increment.

  • @gast128
    @gast128 7 лет назад +2

    adjacent_pair is quite an useful algorithm; please propose or add to Boost. Also I support the suggestion to use good names, but it seems that the STL is a major offender here (e.g. std::remove_copy which has nothing to with std::remove but more like copy_if_not or copy_omit).

    • @jamesflames6987
      @jamesflames6987 4 года назад +5

      std::remove_copy performs the same function as std::remove except it is not in place.

  • @childhood1888
    @childhood1888 2 года назад +1

    36:00

  • @broken_abi6973
    @broken_abi6973 8 лет назад +12

    I find std::algorithms very hard/non-intuitive to use. I have to always go online to refresh my memory on what each algorithm does and the order of its arguments and outputs. I would prefer a more functional programming or range syntax.

    • @neqkk
      @neqkk 8 лет назад +4

      They become much easier to use after reading Alexander Stepanov' Elements of Programming and watching is A9 lectures here on youtube. It was a bit disappointing from the presenter btw not to mention his name in the first place.

    • @JuanGarcia-zy8yw
      @JuanGarcia-zy8yw 7 лет назад +12

      LOLLLLLL with how complex c++ is and you get those beautiful easy to use abstractions and you still complain... bruh...

    • @cmlon
      @cmlon 7 лет назад +10

      He did mention him, at 29:13 and 30:01

    • @JuanGarcia-zy8yw
      @JuanGarcia-zy8yw 7 лет назад +4

      yoooooooo thank you for sharing that im loving his lectures. real in depth analysis with basic examples, and historical facts about programming . so awesome.

    • @edilsongiachini5847
      @edilsongiachini5847 6 лет назад

      u ar dumb

  •  8 лет назад +2

    I'm pretty sure that std::copy example has a bug in it (if 'first' points to an element after 'last' it will break). That for loop should be using "first < last" as the end condition to work around that. (hopefully that's how a real implementation does it).

    • @timpavlic6188
      @timpavlic6188 8 лет назад +25

      Input Iterators are not expected to have operator < defined. As was mentioned, the minimum amount of constraints are placed on types in the STL algorithms. Also, the documentation for std::copy says it will copy the range [first, last). If your parameters cannot represent that range, then you are invoking undefined behaviour. As an example, lets say we have an 8-bit micro with address range 0x00 to 0xFF. I want to copy the very last and first byte of the valid memory range to somewhere else.
      // Please excuse the use of C casts
      uint8_t* first = (uint8_t*)0xFF;
      uint8_t* last = (uint8_t*)0x01;
      uint8_t* out = (uint8_t*)0xAA;
      std::copy(first, last, out);
      This is both valid and well defined on my system, as pointers are 8-bit and will wrap from 0xFF to 0x00, thereby representing a valid copyable range.

    • @yiluan6133
      @yiluan6133 7 лет назад +3

      Ionuț Leonte Also watch around 38:00

  • @X_Baron
    @X_Baron 6 лет назад +2

    I find this clow funn.

  • @Fetrovsky
    @Fetrovsky 8 лет назад +6

    "A templated function" ... I hate that expression.

    • @N00byEdge
      @N00byEdge 7 лет назад +1

      Daniel Jesús Valencia Sánchez Well then maybe this talk is not for you

    • @tobiasfuchs7016
      @tobiasfuchs7016 6 лет назад +7

      I feel you. It's a "function template". It's a template that serves to specify functions. Not a template function, and not a templated function. Just like `std::vector` is not a class, it's a class template. `std::vector` is a class.
      I like the talk very much and recommend it to my students in my course, but many speakers, including some living legends, are rather imprecise about that and it does bother me a tad.

  • @indrajitbanerjee4350
    @indrajitbanerjee4350 4 года назад +1

    There is no such thing as a 'templated function'. It's a 'function template' :)

  • @keyhanjannatkhah6729
    @keyhanjannatkhah6729 2 года назад +1

    C++ has nice features but going to be so complex and unreadable. for system programming the system should be visible and understandable with simple concepts, C is much better in this way. with all respect to all efforts behind C++, unfortunately, it try abstract itself and the whole system in the wrong way. complexity over complexity.

    • @godDIEmanLIVE
      @godDIEmanLIVE 2 года назад +1

      C is a part of CPP so everything you describe is opt-in.

  • @moderncpp
    @moderncpp 2 года назад

    adjacent_pair is very useful 👌

  • @So1dier
    @So1dier 5 лет назад +4

    Would be better if he shown how to use it in the code with an example