random_shuffle(stl算法)打亂順序

random_shuffle (stl算法)打亂順序 

2012-03-31 10:39:11|  分類: 算法 |  標籤: |舉報 |字號 訂閱

   random_shuffle()是個完全通用的算法-適用於內建的數據類型和用戶自定義類型。下面的例子創建了一個有7個字符串對象的向量,它包含一週的天數並使用random_shuffle()打亂他們的排列順序:

#include <string>  
 #include <vector> 
 #include <algorithm> 
 #include <iostream>  
using namespace std; 
 int main()  {  
  vector<string> vs; 
  vs.push_back(string ("Sunday"));  
  vs.push_back (string ("Monday"));
   ...  
  vs.push_back (string ("Saturday"));
  random_shuffle(vs.begin(),
  vs.end()); /* 打亂順序 */  
  for (int i = 0; i << 7; i++)  
     cout<<vs[i]; /* 顯示打亂順序後的元素 */ 
 }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章