Sort three variables: Difference between revisions

C++ simplified logic
(Add Cowgol)
(C++ simplified logic)
Line 487:
 
=={{header|C++}}==
<lang Cpp>#include <iostreamalgorithm>
#include <iostream>
#include <string>
#include <vector>
 
template < class T >
void sort3( T& x, T& y, T& z) {
std::vector<T> v;
v.push_back( x ); v.push_back( y ); v.push_back( z );
std::sort(v.begin(), v.end());
bool b = true;
while( b ) {
b = false;
for( size_t i = 0; i < v.size() - 1; i++ ) {
if( v[i] > v[i+1] ) {
T t = v[i];
v[i] = v[i + 1];
v[i + 1] = t;
b = true;
}
}
}
x = v[0]; y = v[1]; z = v[2];
}
int main(int argc, char* argv[]) {
int xi = 77444, yi = -12, zi = 0;
sort3( xi, yi, zi );
std::cout << xi << "\n" << yi << "\n" << zi << "\n\n";
 
std::string xs, ys, zs;
xs = "lions, tigers, and";
Line 520 ⟶ 510:
sort3( xs, ys, zs );
std::cout << xs << "\n" << ys << "\n" << zs << "\n\n";
 
float xf = 11.3f, yf = -9.7f, zf = 11.17f;
sort3( xf, yf, zf );
std::cout << xf << "\n" << yf << "\n" << zf << "\n\n";
 
return 0;
}
</lang>
Line 532 ⟶ 520:
0
77444
 
(from the "Wizard of OZ")
bears, oh my!
lions, tigers, and
 
-9.7
11.17
125

edits