From 390fabf1f17ceebc03a381f0878ed6e2c232646e Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Tue, 27 Feb 2024 13:19:01 -0500 Subject: [PATCH] adding the multiply example --- lectures/14_stacks_queues/README.md | 57 +++++++++++--------------- lectures/14_stacks_queues/multiply.cpp | 26 ++++++++++++ 2 files changed, 50 insertions(+), 33 deletions(-) create mode 100644 lectures/14_stacks_queues/multiply.cpp diff --git a/lectures/14_stacks_queues/README.md b/lectures/14_stacks_queues/README.md index 2da3d09..81a69e3 100644 --- a/lectures/14_stacks_queues/README.md +++ b/lectures/14_stacks_queues/README.md @@ -39,7 +39,7 @@ public: }; ``` -- See and run this simple functor [example](functor.cpp). +- Compile and run this simple functor [example](functor.cpp). ## 14.2 Why are Functors Useful? @@ -59,7 +59,7 @@ bool float_less(float x, float y) { std::vector my_data = {1.1, 2.2, 3.3, 4.4, 5.5}; ``` -- Remember how we can sort the my_data vector defined above using our own homemade comparison function for sorting: +- Remember how we can sort the *my_data* vector defined above using our own homemade comparison function for sorting: ```cpp std::sort(my_data.begin(),my_data.end(),float_less); @@ -96,48 +96,38 @@ public: example float) that returns a bool. That’s exactly what we need for std::sort! This ultimately does the same thing as our tiny helper homemade compare function! -## 14.3 Another more Complicated Functor Example - -Constructors of function objects can be used to specify internal data for the functor that can then be used -during computation of the function call operator! For example: +## 14.3 Another Functor Example ```cpp -class between_values { +#include + +// functor class +class MultiplyBy { private: - float low, high; + int factor; + public: - between_values(float l, float h) : low(l), high(h) {} - bool operator() (float val) { - return (low <= val && val <= high); + // constructor + MultiplyBy(int factor) : factor(factor) {} + + // overloaded function call operator + int operator()(int x) const { + return x * factor; } }; -``` -- The range between low & high is specified when a functor/an instance of this class is created. We might -have multiple different instances of the between_values functor, each with their own range. Later, when the -functor is used, the query value will be passed in as an argument. The function call operator accepts that -single argument val and compares against the internal data low & high. -- This can be used in combination with STL’s find_if construct. For example: +int main() { + // create an instance of the functor + MultiplyBy multiplyByTwo(2); -```cpp -between_values two_and_four(2,4); -if (std::find_if(my_data.begin(), my_data.end(), two_and_four) != my_data.end()) { - std::cout << "Found a value greater than 2 & less than 4!" << std::endl; + // use the functor as a function + std::cout << "Result of multiplying 5 by 2: " << multiplyByTwo(5) << std::endl; + + return 0; } ``` - Alternatively, we could create the functor without giving it a variable name. And in the use below we also -capture the return value to print out the first item in the vector inside this range. Note that it does not print -all values in the range. - -```cpp -std::vector::iterator itr; -itr = std::find_if(my_data.begin(), my_data.end(), between_values(2,4)); -if (itr != my_data.end()) { - std::cout << "my_data contains " << *itr - << ", a value greater than 2 & less than 4!" << std::endl; -} -``` +- You can compile and run this [example](multiply.cpp). ## 14.4 Additional STL Container Classes: Stacks @@ -239,6 +229,7 @@ int main() { } myQueue.pop(); + // What is the output of this next line? std::cout << "Front element after pop: " << myQueue.front() << std::endl; return 0; diff --git a/lectures/14_stacks_queues/multiply.cpp b/lectures/14_stacks_queues/multiply.cpp new file mode 100644 index 0000000..3bb1886 --- /dev/null +++ b/lectures/14_stacks_queues/multiply.cpp @@ -0,0 +1,26 @@ +#include + +// functor class +class MultiplyBy { +private: + int factor; + +public: + // constructor + MultiplyBy(int factor) : factor(factor) {} + + // overloaded function call operator + int operator()(int x) const { + return x * factor; + } +}; + +int main() { + // create an instance of the functor + MultiplyBy multiplyByTwo(2); + + // use the functor as a function + std::cout << "Result of multiplying 5 by 2: " << multiplyByTwo(5) << std::endl; + + return 0; +}