Sort by Even and Odd numbers

前端 未结 6 1084
故里飘歌
故里飘歌 2021-01-20 16:45

I would like to know is it possible to sort number by even or odd using the std::sort function.

I have the following codes but i am not sure how to implement in the

6条回答
  •  灰色年华
    2021-01-20 17:10

    The catch is that there are two functions you can use.

    template< class BidirectionalIterator, class UnaryPredicate >
    BidirectionalIterator partition( BidirectionalIterator first, BidirectionalIterator last, UnaryPredicate p );
    
    template< class BidirectionalIterator, class UnaryPredicate >
    BidirectionalIterator stable_partition( BidirectionalIterator first, BidirectionalIterator last, UnaryPredicate p );
    

    The functions partition() and stable_partition() reorganize elements of the collection in such a way that all elements for which predicate p returns true will precede all those for which p returns false. It means that the elements will be divided into two ranges:

    • [first, return_value)

    • [return_value, last)

    where return_value is the iterator returned by either of functions. The sequence of elements in the resulting groups is where partition() and stable_partition() differ.

    partition() does not guarantee any particular order within the ranges.

    stable_partition() will keep a relative order of elements before the split. It means that if there are two elements a and b, and a precedes b, and if they both belong to the same group after the split, element a will still precede element b.

    Here is a sample code:

    #include 
    #include 
    #include 
    using namespace std;
    
    bool IsOdd(int i) {
        return (i & 1); //use !(i & 1); if you want to place even elements first in the vector
    }
    
    int main() {
        vector v;
        // set some values and shuffle them:
        for (int i = 1; i<10; ++i)
            v.push_back(i);
        random_shuffle(v.begin(), v.end());
    
        cout << "All elements:";
        for (vector::iterator it = v.begin(); it != v.end(); ++it)
            cout << ' ' << *it;
        cout << endl;
    
        //sort the vector
        cout << "Sorted vector:";
        sort(v.begin(), v.end());
        for (vector::iterator it = v.begin(); it != v.end(); ++it)
            cout << ' ' << *it;
        cout << endl;
    
        //change to partition and see the difference
        vector::iterator bound = stable_partition(v.begin(), v.end(), IsOdd);
    
        // print content:
        cout << "odd elements:";
        for (std::vector::iterator it = v.begin(); it != bound; ++it)
            cout << ' ' << *it;
        cout << endl;
    
        cout << "even elements:";
        for (vector::iterator it = bound; it != v.end(); ++it)
            cout << ' ' << *it;
        cout << endl;
    
        cout << "Sorted odd-even vector:";
        for (vector::iterator it = v.begin(); it != v.end(); ++it)
            cout << ' ' << *it;
        cout << endl;
    
        return 0;
    }
    

    Output:

    All elements: 5 6 2 1 9 4 7 8 3
    Sorted vector: 1 2 3 4 5 6 7 8 9
    odd elements: 1 3 5 7 9
    even elements: 2 4 6 8
    Sorted odd-even vector: 1 3 5 7 9 2 4 6 8
    

    I hope it helps for understanding.

提交回复
热议问题