STL_算法_重排和分区(random_shuffle、partition、stable_partition)

C++ Primer 学习中。。。

 

简单记录下我的学习过程 (代码为主)


//所有容器适用//所有容器适用
random_shuffle(b,e)       //随机重排
random_shuffle(b,e,rand)
partition(b,e,p)          //分区(不稳定)
stable_partition(b,e,p)   //稳定分区,相对顺序不变。
//返回值,为第一个分区的最后一个的后一个位置,也可以说是下个分区的起始位置!


/**------http://blog.csdn.net/u010579068------**/
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<list>
#include<ctime>
#include<deque>
#include<algorithm>
using namespace std;

/*****************************************
//所有容器适用
random_shuffle(b,e)         //随机重排
random_shuffle(b,e,rand)
partition(b,e,p)            //分区(不稳定)
stable_partition(b,e,p)     //稳定分区,相对顺序不变。
                            //返回值,为第一个分区的最后一个的后一个位置,也可以说是下个分区的起始位置!
*****************************************/
/**----------------------------------------------------------------------------------

----------------------------------------------------------------------------------**/
/*************************************************************************************
std::random_shuffle                  所有排序容器适用                       algorithm
--------------------------------------------------------------------------------------
template <class RandomAccessIterator>
  void random_shuffle ( RandomAccessIterator first, RandomAccessIterator last );

template <class RandomAccessIterator, class RandomNumberGenerator>
  void random_shuffle ( RandomAccessIterator first, RandomAccessIterator last,
                        RandomNumberGenerator& rand );
//eg:
template <class RandomAccessIterator, class RandomNumberGenerator>
  void random_shuffle ( RandomAccessIterator first, RandomAccessIterator last,
                        RandomNumberGenerator& rand )
{
  iterator_traits<RandomAccessIterator>::difference_type i, n;
  n = (last-first);
  for (i=n-1; i>0; --i) swap (first[i],first[rand(i+1)]);
}
*************************************************************************************/

/*************************************************************************************
std::partition                   所有排序容器适用                           algorithm
--------------------------------------------------------------------------------------
template <class BidirectionalIterator, class Predicate>
  BidirectionalIterator partition ( BidirectionalIterator first,
                                    BidirectionalIterator last, Predicate pred );
//eg:
template <class BidirectionalIterator, class Predicate>
  BidirectionalIterator partition ( BidirectionalIterator first,
                                    BidirectionalIterator last, Predicate pred )
{
  while (true)
  {
    while (first!=last && pred(*first)) ++first;
    if (first==last--) break;
    while (first!=last && !pred(*last)) --last;
    if (first==last) break;
    swap (*first++,*last);
  }
  return first;
}
*************************************************************************************/

/*************************************************************************************
std::stable_partition                 所有排序容器适用                     algorithm
--------------------------------------------------------------------------------------
template <class BidirectionalIterator, class Predicate>
  BidirectionalIterator stable_partition ( BidirectionalIterator first,
                                           BidirectionalIterator last,
                                           Predicate pred );
//eg:

*************************************************************************************/


// random generator function:
ptrdiff_t myrandom (ptrdiff_t i)
{
    return rand()%i;
}

// pointer object to it:
ptrdiff_t (*p_myrandom)(ptrdiff_t) = myrandom;


template <typename T>
void Print(T& V)
{
    typename T::iterator iter=V.begin();
    while(iter != V.end())
    {
        cout<< *iter++ << " ";
    }
    cout<<endl;
}
int main ()
{
    /**---------------------随机重排----------------------**/
    srand (unsigned(time(NULL)));
    vector<int> myvector;
    vector<int>::iterator it;

    // set some values:
    for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9

    // using built-in random generator:
    random_shuffle ( myvector.begin(), myvector.end() );
    cout << "myvector random_shuffle 1 contains:";
    Print(myvector);

    // using myrandom:
    random_shuffle ( myvector.begin(), myvector.end(), p_myrandom);

    // print out content:
    cout << "myvector random_shuffle 2 contains:";
    Print(myvector);

    cout << endl;
    /**-----------------------分区------------------------**/

    deque<int> mydeque;
    deque<int>::iterator id, bound;

    // set some values:
    for (int i=1; i<10; ++i) mydeque.push_back(i); // 1 2 3 4 5 6 7 8 9

    bound = partition (mydeque.begin(), mydeque.end(), bind2nd(modulus<int>(),2));

    // print out content:
    cout << "odd members:";
    for (id=mydeque.begin(); id!=bound; ++id)
        cout << " " << *id;

    cout << "\neven members:";
    for (id=bound; id!=mydeque.end(); ++id)
        cout << " " << *id;

    cout << endl << endl;

    /**---------------------稳定分区----------------------**/

    int m[]={1,4,3,2,9,6,7,8,5};
    list<int> mylist(m,m+9);
    list<int>::iterator il, lbound;

    // set some values:
//    for (int i=1; i<10; ++i) mylist.push_back(i); // 1 2 3 4 5 6 7 8 9

    lbound = stable_partition (mylist.begin(), mylist.end(), bind2nd(modulus<int>(),2));

    // print out content:
    cout << "odd members:";
    for (il=mylist.begin(); il!=lbound; ++il)
        cout << " " << *il;

    cout << "\neven members:";
    for (il=lbound; il!=mylist.end(); ++il)
        cout << " " << *il;

    cout << endl;


    return 0;
}

myvector random_shuffle 1 contains:6 4 9 7 1 8 3 5 2
myvector random_shuffle 2 contains:3 4 2 6 9 7 8 5 1

odd members: 1 9 3 7 5
even members: 6 4 8 2

odd members: 1 3 9 7 5
even members: 4 2 6 8



你可能感兴趣的:(partition,random_shuffle,STL_算法,重排和分区)