Introduction of STL #6: Functors

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

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

  • @malharjajoo6237
    @malharjajoo6237 7 лет назад +44

    this guy is probably some C++ genius

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

    I find this informative video about functors very clear and goes straight to the point, thanks.

  • @Popart-xh2fd
    @Popart-xh2fd 8 месяцев назад

    For 11:00 the solution is this:
    copy_if(myset.begin(), myset.end(), // source
    back_inserter(d), // destination
    bind(logical_or(),
    bind(greater(), placeholders::_1, 20),
    bind(less(), placeholders::_1, 5)
    )
    );

  • @AdminDarkraiRSK
    @AdminDarkraiRSK 4 года назад

    one of the most underrated channel out there!! Full of knowledge. Huge respect sir!! for your Quality content and knowledge. Even many famous writer doesn't go that smooth!

  • @jingfenghong2312
    @jingfenghong2312 7 лет назад

    your tutoring videos are the best that I have ever watched

  • @k59vijay
    @k59vijay 6 лет назад +22

    HI Sir,
    you are c++ mentor for all.. we are not seeing any new series.. humbly requesting you please come up with new series for C++17 .. eagerly waiting...

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

      Vijay K same, he’s a godsend.

    • @CONGKAMAEL
      @CONGKAMAEL 5 лет назад +5

      He's c++ Yoda! Honestly, his videos have flattened the learning curve of C++ massively for me.

  • @amitatray7782
    @amitatray7782 8 лет назад +1

    Superb... been coding in CPP for a while.. but still learnt a lot from your video..

  • @ryklin1
    @ryklin1 8 лет назад +28

    I'd like to point out that at time 12:50 when discussing functors and the needCopy function, the code is not exactly doing what you say in the video. The code is returning a bool value, therefore when applying to transform, you are back_inserting only 0/1 values where 1 indicates the index of the container in which the value is within range, and 0 means it is not. There for, the deque d will contain the result {1,1,0,0,1} instead of {3,1,12} which is what I expected.
    You can instead code something like this:
    [](int x) {return (x > 20) || (x < 5)? x:-1; }
    or
    if ((x > 20) || (x < 5)) {
    return x;
    }
    return -1;
    but this is not ideal because then how do you handle the -1 return value. It would require an extra step to remove.
    Also, when you go on to discuss the Predicate:
    class NeedToCopy {
    public:
    bool operator() (int x) {
    return (x > 20) || (x < 5);
    }
    };
    1) you need to make the operator under "public:", I tried it and it also returns a boolean so the transform function when running the back_inserter places only 0 or 1 values into the deque

    • @Popart-xh2fd
      @Popart-xh2fd 8 месяцев назад

      I think you need to create an iterator like *vector::const_iterator citr = find_if(vec4.begin(), vec4.end(), isOdd);* and use it in transform as input iterator.

  • @_myron
    @_myron 5 лет назад

    I love these tutorials. Thanks Bo!

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

    I think it is important to understand the difference when passing functions vs functors to STL algorithms ... for functions we just pass the name , while for functors , if not already instantiated , we pass the constructor ( since we want to pass in an object )

  • @nitinjain1325
    @nitinjain1325 7 лет назад

    u r the master of c++

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

    You do a great job explaining this thank you

  • @Pajuju1
    @Pajuju1 11 лет назад +1

    your videos are so great!

  • @BoQianTheProgrammer
    @BoQianTheProgrammer  11 лет назад +7

    I was using vim.

    • @orvvro
      @orvvro 4 года назад

      Back in them dayzzz when the reply function was obscure

  • @lysdexic9129
    @lysdexic9129 8 лет назад +1

    at 15.45 would the copy_if algorithm be a better choice? it would self-document the code

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

    pretty good tutorial, helped me a lot :)

  • @skybo053
    @skybo053 8 лет назад +1

    When using the non-type template function "addValue", the for_each compiles fine if you make the x "const"

  • @neetisharma3768
    @neetisharma3768 4 года назад

    Thank you.

  • @BoQianTheProgrammer
    @BoQianTheProgrammer  11 лет назад +1

    Thanks for point out the missing ().

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

    Hello
    I have a question (at time 10:00 ) regarding the conversion of the regular function (pow) to a function object "using the function template ( std::function )
    I tried to skip this conversion, and use right in the call of the std::bind() and it works fine
    As shown below:
    using namespace std;
    int power(int x, int y)
    {
    return pow(x,y);
    }
    int main()
    {
    set s1 = {3,4,5,6,7,8};
    for(int i : s1)
    cout

  • @BoQianTheProgrammer
    @BoQianTheProgrammer  11 лет назад

    That's a C++ 11 feature, and it is not available on VS2010, not even on VS2012. You can use it with GCC 4.7 or 4.7, or Clang.

  • @mohankhanzode9819
    @mohankhanzode9819 11 лет назад

    you need to use reference while passing the integer from function, otherwise it won't have any affect on caller function, rest is fine. Thanks

  • @leelomchen3119
    @leelomchen3119 6 лет назад +1

    50″ - functor expands the concept of function by saying anything behave like a function is a function

  • @blackbox39999
    @blackbox39999 11 лет назад

    can you upload a video on priority_queue. How to handle a class objecs in a priority queue.

  • @eelapata
    @eelapata 7 лет назад

    Superb

  • @seejama
    @seejama 3 года назад

    At 15.00, The functor takes 2 parameters where as template needs function with one parameter. Don't you have to use bind?

  • @BoQianTheProgrammer
    @BoQianTheProgrammer  11 лет назад

    Yes, I will do that.

  • @meninja111
    @meninja111 11 лет назад

    okk sir. thank you.

  • @VivekYadav-ds8oz
    @VivekYadav-ds8oz 5 лет назад +1

    I still see absolutely no use for functors other than the fact that C++ STL requires us to use it.
    Also, I don't see any point in how addValue(2)(x) is any better than addValue(2, x), both are very readable to me. If anything, I feel like the latter will be faster to execute than former, due to the classes being involved now.
    Just why..? Is style of coding that much more dominant of a factor than actual efficiency?
    Is there ANYTHING I'm missing out on or don't get, please tell me, but I don't see any point in making functors. Why does STL library not take function pointers and only functors? What's the advantage of that?

  • @azadalishah2966
    @azadalishah2966 5 лет назад

    At 12:10, in bind(logical_or, ...) why there is no () ie. logical_or() like other templated functors ?

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

    Transform() can take binary function / functor as it's last object..

  • @gg-ij8xb
    @gg-ij8xb 4 года назад

    samples and notes of this tutorials: github.com/RamazanDemirci/cppAdvancedSTL

  • @Joe-xj9by
    @Joe-xj9by 7 лет назад

    Could someone please clarify for me what is meant by the empty parenthesis in : int x = multiplies()(3,4)?

    • @aladicstamas6023
      @aladicstamas6023 7 лет назад

      I don't know if you got it by now, but the empty parenthesis is the constructor call, the second is the operator(). So basically it creates a multiplier functor by calling the default constructor, and then it calls operator() with the parameters 3 and 4.

  • @meninja111
    @meninja111 11 лет назад

    which editor you are using sir ??

  • @SHUBHAM-rn8zd
    @SHUBHAM-rn8zd 7 лет назад

    at 10.15 you converted pow into functor, why can't you convert lsb_less into functor and use in set at 14.50?

  • @kaushalshah5048
    @kaushalshah5048 8 лет назад

    Is this video about functor or bind function?

  • @dayone1992
    @dayone1992 8 лет назад

    I've used these no i know better, thx

  • @Iceosaur
    @Iceosaur 11 лет назад

    you always initialize vector in this way: vec={2,3,4,6}; but this cannot be compiled in visual studio 2010. Is this syntax legal? or you are using a different compiler? Thanks!
    BTW, at time 11:41, logical_or missing"()".

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

    Thanks, Qian, your tutorials help me! Can you write basic project from scratch?

  • @spicytuna08
    @spicytuna08 6 лет назад +1

    i don't understand how functor is called without instantiation of an object.

    • @azadalishah2966
      @azadalishah2966 5 лет назад +1

      spicytuna08 when u do X() , temporary object of X is created first then invoke functor ie. operator()

  • @freizagen
    @freizagen 9 лет назад

    It should be Lsb_less or Lsb_less() at 14:57. And what is the difference between the two?

    • @Popart-xh2fd
      @Popart-xh2fd 8 месяцев назад

      You need to make the operator method const and then call it as Lsb_less without (), like this:
      struct Lsb_less { // a functor
      bool operator()(int x, int y) const {
      return x%10 < y%10;
      }
      };

  • @mohammedhakeem1189
    @mohammedhakeem1189 8 лет назад

    auto f = function

    • @mohammedhakeem1189
      @mohammedhakeem1189 8 лет назад

      +peterolen
      cod works well using this line : function f = function(Pow);
      not this : auto f = function(pow);
      or this : function f = Pow;

  • @Tadeletad
    @Tadeletad 7 лет назад

    no alien life on space, qomche said.

  • @ldxyz-s1e
    @ldxyz-s1e 7 лет назад

    8:09

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

    And they said functors were hard to understand...

  • @loia5tqd001
    @loia5tqd001 5 лет назад +1

    Awwwww too complex C++

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

    transform(myset.begin(), myset.end(), //source
    back_inserter(d), //destination
    bind(greater(), placeholders::_1, 20),
    bind(less(), placeholders::_1, 5));
    Error compiler C2675
    C2675 unary '++': 'std::_Binder' does not define this operator or a conversion to a type acceptable to the predefined operator STL7 Functor c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.25017\include\algorithm 996

  • @spirridd
    @spirridd 8 лет назад +1

    The video is hardly understandable, the previous ones were much better.