123456789101112131415161718192021222324252627
#include<iostream> #include<vector> #include<algorithm> using namespace std; template<typename comparable> void print(const comparable& a,const std::string& optstr=""){ cout<<optstr; for(auto &elem:a){ cout<<elem;} cout<<endl; }//print function bool stcrit(int x,int y){ if((x%2) > (y%2)){ return x<y; } else{ return x>y;} } int main(){ vector<int> v1{5,4,2,9,7,6,1}; sort(v1.begin(),v1.end(),stcrit); print(v1); return 0; }
123456789101112131415
#include <iostream> bool stcrit(int x,int y) { if( (x%2) > (y%2) ) return x<y; else return x>y; } int main() { std::cout << std::boolalpha << "stcrit(1,4): " << stcrit(1,4) << '\n' // true << "stcrit(4,1): " << stcrit(4,1) << '\n' ; // true }