Reverse function

Hello,

I had this exercise:

Write a function that reverses the order of elements in a vector<int>.
For example, 1,3,5,7,9 becomes 9,7,5,3,1. The function should reverse the elements of its vector without using any other vectors (hint:swap).

I need some ideas. This didn`t work out:

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
29
30
31
32
33
34
35
36
 #include <iostream>
#include <vector>

using namespace std;


void print(vector<int>&v)  {

 cout<< "{"; 
for (int x = 0; x<v.size(); ++x) { cout << v[x]; } cout << "}\n";

}

void reverse (vector<int>&v,int&d1,int&d2) {

for  (int i=2; i<v.size(); ++i) 

{  int temp=d1; d1=d2; v.push_back(d1); d1=d2; d2=temp; }

}


int main()

{


cout<< "please enter some integer values (to be swapped) : ";
int w;
int a;
vector<int>vf;

while (cin>>w) { vf.push_back(w); reverse(vf,w,a); print(vf);}

	
}
Two variables one starts at the other starts at v.size()-1
1
2
3
4
5
while(var1 < var2) {
    //swap v[var1] and v[var2]
    var1++;
    var2--;
}

What exactly are d1 and d2 doing in the function? Are you intending to only reverse the elements indexed from [d1, d2] inclusively? If so, use d1 and d2 instead of the values I said at the top.
Topic archived. No new replies allowed.