questions about stl sort

After learning Sorting Algorithm,i have a thought on the criterion.
criterion: if x=odd,y=even;
x is large;
else
return x>y;
so i write the code below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#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;
}

it output"9542716".
it is so strange that i have no idea on it.
Would you do me a favor,thanks a lot.
The predicate does not meet the requirements of Compare
https://en.cppreference.com/w/cpp/named_req/Compare

For instance, both stcrit(1,4) and stcrit(4,1) would yield true.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#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
}

http://coliru.stacked-crooked.com/a/d1afde8955d7430c
excellent explanation. get the point.
thanks a lot.
Last edited on
Topic archived. No new replies allowed.