STL演算法 -------- 重排演算法、分割槽演算法
阿新 • • 發佈:2019-01-03
1. random_shuffle() 隨機打亂
2. partition() 不穩定的
3. stable_partition() 穩定的
#include <iostream> #include <algorithm> #include <vector> #include <iterator> #include <functional> using namespace std; template <typename T> void Print(const T& t) { for(typename T::const_iterator itr=t.begin(); itr!=t.end(); ++itr) { cout<<*itr<<' '; }cout<<endl; } int main( int argc, char** argv ) { vector<int> vec; vector<int> vec1; vector<int> vec2; for(int i=1; i<=9; ++i) { vec.push_back(i); vec1.push_back(i); vec2.push_back(i); } Print(vec); random_shuffle(vec.begin(), vec.end()); Print(vec); vector<int>::iterator pos; cout<<endl<<"vec1:"<<endl; Print(vec1); //pos = partition(vec2.begin(), vec2.end(), not1(bind2nd(modulus<int>(),2)) ); pos = partition(vec1.begin(), vec1.end(), bind2nd(modulus<int>(),2) ); Print(vec1); cout<<endl<<"vec2:"<<endl; Print(vec2); stable_partition(vec2.begin(), vec2.end(), bind2nd(modulus<int>(), 2)); Print(vec2); return 0; }