iter-swap help

I'm not quite convinced about the output 1 5 9 6 2 4 7 8 3 1.
Can someone clarify? I wrote some notes on the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <deque>
#include <list>
#include <algorithm>
#include <set>
#include <queue>
#include <vector>
#include <map>
using namespace std;

void myfunction(int i){
    cout << " " << i;
}

void multiply(int a){
    a*2;
}
int main(){
    int t[]={10,5,9,6,2,4,7,8,3,1};
    vector<int>v1(t,t+10); // v1 now contains the same elements as t[]
    for_each(v1.begin(),v1.end(),multiply); // for each element in v1, the function multiply is applied, hence they are now 20,10,18,12,etc
    iter_swap(v1.begin(),t+9); // the first element of v1, which is 20, is swapped with the last element of t[] which is 1. 
    for_each(v1.begin(),v1.end(),myfunction); // From my understanding the output shoud be 1,10,18,12,4,8,14,16,6,2. But it was not the case. Why?.... help
      
  
    return 0;

}
your function multiply takes its argument by value, multiplies it, and discards the result. I believe your intent was to pass by reference:
1
2
3
void multiply(int& a){
    a*=2;
}
Topic archived. No new replies allowed.