From the course: C++ Templates and the STL
Unlock this course with a free trial
Join today to access over 22,400 courses taught by industry experts.
Arithmetic functors - C++ Tutorial
From the course: C++ Templates and the STL
Arithmetic functors
- The arithmetic functors are standard template functors defined in the functional header, it's right there, of the STL. Here I have a working copy of arithmetic.cpp from chapter five of the exercise files. And I've defined down here three vectors, v1, v2, which have some numbers that we'll do arithmetic on, and v3 for the results. I have a functor defined, this is the plus functor, I've created an object from the class, course if we wanted to we could use the functor directly like this with parentheses with a function call operator. Really to get the full value of a functor, you'll want to use it as an object. So when I build and run, you'll notice that the plus functor adds these numbers together, so 26 plus one is 27, 52 plus two is 54, et cetera. The minus functor, will subtract the two numbers. So we're using the binary version of transform here. It has the five arguments. And here we got the subtraction. We can multiply with the multiplies functor. Build and run we have the…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.