83

This is my swap function:

template <typename t>
void swap (t& x, t& y)
{
    t temp = x;
    x = y;
    y = temp;
    return;
}

And this is my function (on a side note v stores strings) call to swap values but whenever I try to call using values in a vector I get an error. I'm not sure what I'm doing wrong.

swap(v[position], v[nextposition]); //creates errors
4
  • 6
    Do you have somewhere using namespace std in your code? Because you could have a name conflict.
    – Benoit
    Commented Jun 3, 2011 at 8:47
  • 10
    BTW You don't need that return; statement at the end of your function Commented Jun 3, 2011 at 9:02
  • My best guess is that you are implementing a quadratic sort algorithm, and you have an off-by-one error, thus trying to access the vector out of bounds. It would really help to see the actual error message. Is it a compile-time error or a runtime error? Commented Jun 3, 2011 at 9:07
  • 6
    Why aren't you just using std::swap? Commented Jun 3, 2011 at 13:33

5 Answers 5

149

I think what you are looking for is iter_swap which you can find also in <algorithm>.
all you need to do is just pass two iterators each pointing at one of the elements you want to exchange.
since you have the position of the two elements, you can do something like this:

// assuming your vector is called v
iter_swap(v.begin() + position, v.begin() + next_position);
// position, next_position are the indices of the elements you want to swap
4
  • 9
    I had this same issue, and there are a lot of answers recommending std::swap (like the highest voted one here does) but std::swap does not work (directly) for swapping the contents of two elements of a vector. Your answer, with std::iter_swap does.
    – latreides
    Commented Jan 9, 2014 at 20:23
  • 2
    @latreides Could you please clarify what you mean by "std::swap does not work (directly) for swapping the contents of two elements of a vector"? Commented Jan 16, 2016 at 6:44
  • @ChrisCulter Its been awhile since I made that comment (a little over two years), all I can recall is that std::swap did not give me the behavior I wanted (or expected) but std::iter_swap did.
    – latreides
    Commented Jan 17, 2016 at 2:07
  • 9
    Note that std::iter_swap(it1, it2) is equivalent to std::swap(*it1, *it2). Commented May 5, 2016 at 8:32
76

Both proposed possibilities (std::swap and std::iter_swap) work, they just have a slightly different syntax. Let's swap a vector's first and second element, v[0] and v[1].

We can swap based on the objects contents:

std::swap(v[0],v[1]);

Or swap based on the underlying iterator:

std::iter_swap(v.begin(),v.begin()+1);

Try it:

int main() {
  int arr[] = {1,2,3,4,5,6,7,8,9};
  std::vector<int> * v = new std::vector<int>(arr, arr + sizeof(arr) / sizeof(arr[0]));
  // put one of the above swap lines here
  // ..
  for (std::vector<int>::iterator i=v->begin(); i!=v->end(); i++)
    std::cout << *i << " ";
  std::cout << std::endl;
}

Both times you get the first two elements swapped:

2 1 3 4 5 6 7 8 9
2
  • std::swap() does not work for iterators, consider using std::iter_swap() instead as it was suggested in another answer on this page.
    – Mar
    Commented Mar 12, 2016 at 3:57
  • 3
    @Mar As suggested by Superfly: Note that std::iter_swap(it1, it2) is equivalent to std::swap(*it1, *it2). So std::swap can work just fine. Commented Sep 16, 2016 at 21:07
26

There is a std::swap in <algorithm>

1
  • 2
    ...for swapping out the contents of 2 separate vectors!
    – Nate T
    Commented Sep 20, 2021 at 17:04
6

after passing the vector by reference

swap(vector[position],vector[otherPosition]);

will produce the expected result.

3
  1. Using std::swap by including the <algorithm> library to swap values by references / smart pointers,
    e.g. std::swap(v[0], v[1])
    Note: v[i] is a reference

  2. Using std::iter_swap from the same library,
    e.g. std::iter_swap(v.begin(), v.begin() + v.size() - 1)

  3. Using lvalue references and rvalue tuple by including the <tuple> library
    e.g. std::tie(v[0], v[1]) = std::make_tuple(v[1], v[0])
    Note: constexpr since C++14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.