How is x ^= y; y ^= x; x ^= y; unsafe when x and y are equally sized integers? Is there a problem with using xor for signed integers?
it has the same overflow problem that my template has.
Well, I wouldn't really use it, because I've never swapped integers in my life.
Are you still a student? If not, how long have you been programming? Swapping integers (and other data types) is a common thing in sorting algorithms.
As for swapping strings, there is a member function named 'swap' which tends to achieve the task in constant time.
probably implemented like this
void swap(std::string& s1, std::string& s2)
{
std::string temp = s1;
s1 = s2;
s2 = temp;
}
I know of no other way to do it with std::strings.