0

I'm in the process of completing a school assignment and I'm having a problem using swap.h. I know that normally things like this are external dependencies but in Visual Studio 2010, I don't see it in my list of external dependencies. I'm not sure where to find it or how to add it to the list so I can use the swap function. Is there anyone who can point me in the right direction here?

#include "swap.h"

That's just to simply show how I'm attempting to include the file.

Heisenberg
  • 37
  • 2
  • 12

2 Answers2

4

so I can use the swap function

std::swap() from the C++ Algorithm library is

  • Defined in header <algorithm> (until C++11)
  • Defined in header <utility> (since C++11)

In Visual Studio 2010, you probably need:

#include <algorithm>

Following comments:

Call std::swap() thus, to enable :

using std::swap;
swap(theArray[index], theArray[nextIndex]);

Further reading: How to provide a swap function for my class?

Community
  • 1
  • 1
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • Thank you for your solution but when I do this, I'm getting an error when calling the swap function. – Heisenberg Mar 03 '13 at 23:27
  • I'm calling it like this: swap(theArray[index], theArray[nextIndex]); – Heisenberg Mar 03 '13 at 23:28
  • It's saying the identifier "swap" is undefined. – Heisenberg Mar 03 '13 at 23:28
  • 1
    `swap` is in the `std` namespace, use `std::swap` (do not use `using namespace std`, it's not good practice) – Stephen Lin Mar 03 '13 at 23:30
  • @StephenLin Thank you, this fixed the problem. I appreciate all the help from everyone. – Heisenberg Mar 03 '13 at 23:31
  • 1
    Usually he best way to call swap is with `using std::swap;` ... `swap(a,b);`. Then the type can implement it as an overload found via ADL. The only reason not to do that, is if you're swapping a type whose implementer (for some strange reason) has given it a non-member function named `swap` that does something other than swapping. – Steve Jessop Mar 03 '13 at 23:32
  • 1
    @SteveJessop yes, but that might be bit too much for a school assignment...I doubt this is supposed to robust against new user-defined types defining `swap` overloads in their own namespaces...good point though, in general – Stephen Lin Mar 04 '13 at 00:10
  • 1
    @StephenLin: you're right, but unless the questioner doesn't have time to look up the meaning of what I'm talking about, then there's no time like now to start doing things properly :-) I just mentioned it because the questioner doesn't specify the type of the objects swapped. If it's some type out of a library then there's a genuine risk that `std::swap` will be inefficient and can throw. If it's `int` then fully-qualified `std::swap` is absolutely fine. – Steve Jessop Mar 04 '13 at 00:43
  • @StephenLin: Additionally, we want StackOverflow answers to be useful to future visitors as well as original posters. – johnsyweb Mar 04 '13 at 00:54
  • 1
    @Johnsyweb hah no worries, I definitely agree :) it was just the easiest thing to explain in a comment so the OP could compile – Stephen Lin Mar 04 '13 at 00:57
  • @StephenLin: Indeed. I'm always happy for [people to edit](http://stackoverflow.com/privileges/edit) my posts if it helps someone solve a problem :) – johnsyweb Mar 04 '13 at 01:00
1

include <algorithm> to get std::swap

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331