or if you need to implement this yourself, consider how std::reverse does it.
Given an iterator to the first element and an iterator to the last, it swaps
the elements pointed to by the iterators. Then it moves the first iterator
'right' one and the last iterator 'left' one and repeats until first==last or
first 'passes by' last.
I've just tried std:Reverse and it works, but I'm trying to make my own function.
Maybe I'm on the wrong track, but from my understanding my function in the original post tries to imitate the the way Reverse works(?):
1 2 3 4 5 6 7 8 9 10 11 12 13
//if divisible by 2:
if(v1.size()%2==0)
//Enter a for-loop where i is the iterator to the first element
for (int i = 0; i <= v1.size(); i++){
//make a buffer called temp and give it the value of the first element.
int temp = v1[i];
// Give the first element the value of the last element
v1[i] = v1[v1.size()-i];
//Give the last element the value in temp
v1[v1.size()-i] = temp;
//Stop the swapping from swapping values already swapped :-)
if(v1.size()-1==i)
break;}
So i increases and the 2nd value and 2nd to last value gets swapped and so on.
The second bit: (if size%2 != 0) is intended to to the same, except it leaves the middle element who has no "partner" to stay in its place.
I know without even looking at std::reverse()'s implementation that your function does not imitate what it does, because std::reverse() is not going to know the number of elements are in the range it needs to reverse.
You don't need to make cases for size % 2 == 1 and size % 2 == 0. Not making the special case will help simplify your development and testing since you will now have only one case to test.
First, consider line 12. It says, "stop flipping when I come to an element I already flipped". In the case of an even number of elements in the list, the (size/2)th element (assuming zero based) is the first element you'll encounter that you already flipped. In the odd case, size/2 has remainder 1, but still the (size/2)th element [integer division] is the first element you'll encounter that you already flipped.
So this to me means that I can write the correct terminating condition in the for() loop directly:
1 2 3
for( vector<int>::size_type i = 0; i < v1.size() / 2; ++i )
// swap element i with element size() - i - 1. you already wrote this code twice.
std::swap( v1[i], v1[v1.size()-i-1] );
You'll see that the author has given you a small function that gives you an idea of how it might work using iterators. They key word here is "iterators". Can you understand why? It is because std:reverse works for more than just a vector where your function requires a vector. You could use std::reverse for many kinds of containers, including a C style array which is just a block of memory. It is admirable that you are trying to improve your skills by writing some code and studying but I think that you are taking the wrong approach here. Your function is not as versatile as the std library implementation. In some cases I think it is better to just work with the std library and learn how to use it instead of trying to rewrite it.
Thank you very much for pointing out my logic failures and spelling it out.
With your help the function looks like:
1 2 3 4 5 6 7 8 9 10
void swap_vector(vector<int>&v1)
{
for( int i = 0; i < v1.size() / 2; ++i ){
int temp = v1[i];
v1[i] = v1[v1.size()-i-1];
v1[v1.size()-i-1] = temp;
}
Does what it is supposed to do, too!
kempofighter:
Thank you for the link.
I'm still taking baby steps, so I'd have to disagree. Getting your ha... uhm...fingers dirty at this point helps or at least I'd like to think so. However, I do know the usage of the simple homebrew functions are limited, but until I have more experience under the belt I'll dissect my own frogs - or have the people here hold my hand doing it, hehe.