I am practicing function pointers in C++. I have written the following code. I have declared an integer vector and added values to it. After that I am passing the values of vector to a function by reference. I am adding the value to each value of vector. After that when I display the content of original vector, the values does not change. Following is the code.
void printValues (int val) {
cout << val << " ";
}
void ForEach (vector<int> values, void (* func )(int), int inc) {
for (int value : values) {
value = value + inc;
func (value);
}
}
int main()
{
vector<int> v1;
cout << "Please enter the values in vector";
for (int i = 0; i < 5; i++) {
int val = 0;
cin >> val;
v1.push_back(val);
}
cout << "value stored in vector :" ;
ForEach(v1, printValues,8);
cout << "\nThe content of original vector:";
for (int i = 0; i < v1.size(); i++) {
cout << " " << v1[i];
}
}
I expect the output to be 58,68,78,88,98, but the actual output is 50,60,70,80,90.