inplace_merge
#include <algorithm>
#include <iostream>
#include <vector>
using std::vector,
std::inplace_merge,
std::sort,
std::advance,
std::for_each,
std::cerr;
class Temp{
public:
int a, b;
Temp(int i, int j):a(i), b(j){}
};
bool operator<(const Temp& l, const Temp& r)
{
if((l.a + l.b) < (r.a + r.b)){
return true;
}
return false;
}
typedef vector<Temp> my_vector;
int main(){
my_vector a = {Temp(16, 256), Temp(4, 16), Temp(12, 144), Temp(4, 16), Temp(19, 361), Temp(9, 81), Temp(3, 9), Temp(15, 225), Temp(5, 25), Temp(6, 36), Temp(1, 1), Temp(8, 64), Temp(13, 169), Temp(0, 0), Temp(2, 4), Temp(2, 4), Temp(3, 9), Temp(10, 100), Temp(18, 324), Temp(14, 196), Temp(17, 289), Temp(7, 49), Temp(11, 121)};
auto print = [] (Temp t){
cerr << "Temp(" << t.a << ", " << t.b << ")\n";
};
auto test = [] (Temp a, Temp b)->bool{
if(a.a < b.a && a.b < b.b){
return true;
}
return false;
};
sort(a.begin(), a.end(), test);
my_vector::iterator middle = a.begin();
advance(middle, a.size()/2);
inplace_merge(a.begin(), middle, a.end());
for_each(a.begin(), a.end(), print);
inplace_merge(a.begin(), middle, a.end(), test);
for_each(a.begin(), a.end(), print);
return 0;
}
C++ Examples© 2024 TBD