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!
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...
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
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.
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 )
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
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?
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.
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"()".
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; } };
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
this guy is probably some C++ genius
beast
"probably"?
@@bluehornet6752 absolutely!
He's the ONE!
I find this informative video about functors very clear and goes straight to the point, thanks.
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)
)
);
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!
your tutoring videos are the best that I have ever watched
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...
Vijay K same, he’s a godsend.
He's c++ Yoda! Honestly, his videos have flattened the learning curve of C++ massively for me.
Superb... been coding in CPP for a while.. but still learnt a lot from your video..
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
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.
I love these tutorials. Thanks Bo!
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 )
u r the master of c++
You do a great job explaining this thank you
your videos are so great!
I was using vim.
Back in them dayzzz when the reply function was obscure
at 15.45 would the copy_if algorithm be a better choice? it would self-document the code
pretty good tutorial, helped me a lot :)
When using the non-type template function "addValue", the for_each compiles fine if you make the x "const"
Thank you.
Thanks for point out the missing ().
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
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.
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
50″ - functor expands the concept of function by saying anything behave like a function is a function
can you upload a video on priority_queue. How to handle a class objecs in a priority queue.
Superb
At 15.00, The functor takes 2 parameters where as template needs function with one parameter. Don't you have to use bind?
Yes, I will do that.
okk sir. thank you.
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?
At 12:10, in bind(logical_or, ...) why there is no () ie. logical_or() like other templated functors ?
really need ()
Transform() can take binary function / functor as it's last object..
samples and notes of this tutorials: github.com/RamazanDemirci/cppAdvancedSTL
Could someone please clarify for me what is meant by the empty parenthesis in : int x = multiplies()(3,4)?
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.
which editor you are using sir ??
at 10.15 you converted pow into functor, why can't you convert lsb_less into functor and use in set at 14.50?
Is this video about functor or bind function?
I've used these no i know better, thx
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"()".
Thanks, Qian, your tutorials help me! Can you write basic project from scratch?
i don't understand how functor is called without instantiation of an object.
spicytuna08 when u do X() , temporary object of X is created first then invoke functor ie. operator()
It should be Lsb_less or Lsb_less() at 14:57. And what is the difference between the two?
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;
}
};
auto f = function
+peterolen
cod works well using this line : function f = function(Pow);
not this : auto f = function(pow);
or this : function f = Pow;
no alien life on space, qomche said.
8:09
And they said functors were hard to understand...
Awwwww too complex C++
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
The video is hardly understandable, the previous ones were much better.