Feb 10, 2018 at 8:12pm UTC
So this assignment asks us to swap the third value and the last value of a vector if the length of the vector is >=3. Somehow my code isn't working.
#include <iostream>
#include <vector>
using namespace std;
void swap3last(vector<int>& a)
{
if(a.size()>=3)
iter_swap(a.begin()+2, a.end());
}
Can someone help me with that
Feb 10, 2018 at 8:23pm UTC
The std::vector.end() doesn't point to the last element. It points to one past the end of the vector.
Feb 10, 2018 at 8:37pm UTC
I switched it to a.back(), it still doesn't work
Feb 10, 2018 at 9:02pm UTC
the code does not work for any size, or only if size = 3?
if size = 3, then
a.begin ()+ 2 = a.end () ;
then do not swap..
Feb 11, 2018 at 12:27am UTC
It gives me this error
void swap3last(vector<int>& a)
{
int i;
for(i=0;i<a.size();i++)
{if(a.size()>=3)
iter_swap(a.begin()+2, a.back());} //invalid type argument of unary β*β (have βintβ)//
}
Feb 11, 2018 at 1:05am UTC
never mind i got it. thanks jlb!