#include <iostream>
#include <set>
#include <algorithm>
using namespace std;
void sort_three0(int &a, int &b, int &c) {
vector<int>v{a, b, c};
sort(begin(v), end(v));
a = v[0];
b = v[1];
c = v[2];
}
void sort_three1(int &a, int &b, int &c) {
int min_v = min(min(a, b), c);
int max_v = max(max(a, b), c);
vector<int>v{a, b, c};
for (auto &e : v) {
if (e != min_v && e != max_v) {
b = e;
break;
}
}
a = min_v;
c = max_v;
}
void sort_three2(int &a, int &b, int &c) {
set<int>ss{a, b, c};
auto it = begin(ss);
a = *it;
b = *(++it);
c = *(++it);
}
int main() {
int a = 10;
int b = 0;
int c = -10;
for (int i = 0;i < 1000000;i++) {
//sort_three0(a, b, c); // 0.523s
sort_three1(a, b, c); // 0.334s
//sort_three2(a, b, c); // 1.1098s
}
return 0;
}
标签:sort,min,int,max,void,three,number,include From: https://blog.51cto.com/u_15899033/5902957