adding the multiply example
This commit is contained in:
@@ -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?
|
## 14.2 Why are Functors Useful?
|
||||||
|
|
||||||
@@ -59,7 +59,7 @@ bool float_less(float x, float y) {
|
|||||||
std::vector<float> my_data = {1.1, 2.2, 3.3, 4.4, 5.5};
|
std::vector<float> 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
|
```cpp
|
||||||
std::sort(my_data.begin(),my_data.end(),float_less);
|
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
|
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!
|
same thing as our tiny helper homemade compare function!
|
||||||
|
|
||||||
## 14.3 Another more Complicated Functor Example
|
## 14.3 Another 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:
|
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
class between_values {
|
#include <iostream>
|
||||||
|
|
||||||
|
// functor class
|
||||||
|
class MultiplyBy {
|
||||||
private:
|
private:
|
||||||
float low, high;
|
int factor;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
between_values(float l, float h) : low(l), high(h) {}
|
// constructor
|
||||||
bool operator() (float val) {
|
MultiplyBy(int factor) : factor(factor) {}
|
||||||
return (low <= val && val <= high);
|
|
||||||
|
// 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
|
int main() {
|
||||||
have multiple different instances of the between_values functor, each with their own range. Later, when the
|
// create an instance of the functor
|
||||||
functor is used, the query value will be passed in as an argument. The function call operator accepts that
|
MultiplyBy multiplyByTwo(2);
|
||||||
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:
|
|
||||||
|
|
||||||
```cpp
|
// use the functor as a function
|
||||||
between_values two_and_four(2,4);
|
std::cout << "Result of multiplying 5 by 2: " << multiplyByTwo(5) << std::endl;
|
||||||
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;
|
return 0;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Alternatively, we could create the functor without giving it a variable name. And in the use below we also
|
- You can compile and run this [example](multiply.cpp).
|
||||||
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<float>::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;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## 14.4 Additional STL Container Classes: Stacks
|
## 14.4 Additional STL Container Classes: Stacks
|
||||||
|
|
||||||
@@ -239,6 +229,7 @@ int main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
myQueue.pop();
|
myQueue.pop();
|
||||||
|
// What is the output of this next line?
|
||||||
std::cout << "Front element after pop: " << myQueue.front() << std::endl;
|
std::cout << "Front element after pop: " << myQueue.front() << std::endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
26
lectures/14_stacks_queues/multiply.cpp
Normal file
26
lectures/14_stacks_queues/multiply.cpp
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user