I'm writing a program that requires me to sort a vector of objects. I already know how to compare two objects in the vector based on a member variable, but how would I swap the two objects in the vector?
As zhuge said, you can easily sort stuff with std::sort.
Also, there is std::swap to swap two values - vectors also have a member function swap, however it swaps out values between two vectors, not two values in the same vector.
Anyways, swapping values in general can be done like that:
I would rather not use an outside function.
I want to do know how to do it without using that header file.
I've got the sorting function all written out except for the swap part.
As I said, std::swap or just swap the values normally... Create a temporary variable that stores one of the values, then assign one value to the other, and then assign the temporary value to the first.
I just want to be clear. You guys know I'm not sorting a vector of a simple data type, right? It's a vector of a class object. I just tried the temporary variable thing and it didn't work.