I have the following program where I am trying to print out multiplication table of 10, using a combination of lambda functions
and std::bind
. The primary purpose is to learn the above two concepts. However the output that I get (given below) is strange.
#include <iostream>
#include <vector>
#include <algorithm>
#include <thread>
#include <zconf.h>
#include <functional>
using namespace std;
using namespace std::placeholders;
int multiply(int a, int b, int c)
{
return a*b*b;
}
int main()
{
auto f = std::bind(multiply, 5 , 2 , _1);
vector<int> vec = {1,2,3,4,5,6,7,8,9,10};
for_each(vec.begin(), vec.end(), [f](int &v) {cout << "Multiplication Table (10) :" << "v: " << v << " " << f(v) << endl;});
return 0;
}
Output:
Multiplication Table (10) :v: 1 20
Multiplication Table (10) :v: 2 20
Multiplication Table (10) :v: 3 20
Multiplication Table (10) :v: 4 20
Multiplication Table (10) :v: 5 20
Multiplication Table (10) :v: 6 20
Multiplication Table (10) :v: 7 20
Multiplication Table (10) :v: 8 20
Multiplication Table (10) :v: 9 20
Multiplication Table (10) :v: 10 20
Process finished with exit code 0
What is it in the above code that I am doing wrong. What should be fixed so that it actually prints the multiplication table for 10.
Looks like a simple typo to me. Change
return a*b*b;
to
return a*b*c;