C++ vectors

Hello, i need some help with vectors, doing one excersise from the book.
i need to rearange the vector that it would be saved in reverse order.
my vector is pass by refference with & so it should change its values if changes is made in the function. Why my vector after function keeps the same
old values and do not save new values passed?

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
#include <iostream>
#include <vector>

void reverse(std::vector<int>& v) 
{
	int a;
	int b;
	int temp;
	int lenght = v.size();
	for (int i = 0; i < lenght; i++)
	{
		a = v[i];
		b = v[lenght - 1 -i];
		temp = a;
		v[i] = b;
		v[lenght - 1 - i] = temp;
	}
	for (int i = 0; i < lenght; i++)
	{
		std::cout << v[i] << std::endl;
	}
}
int main()
{

	std::vector<int> v{ 1,2,3,4,5,6 };
	reverse(v);
	std::cout << v[1] << std::endl;
	system("pause");
	return 0;
}
Step through your algorithm for just two elements (e.g. {1, 2}) and you'll understand why the order ends up being the same as before.
This might make it clear.
https://pasteboard.co/HKwGeaE.png
Thanks, now its working.
Topic archived. No new replies allowed.