1. 程式人生 > >C++ STL vector的亂序演算法

C++ STL vector的亂序演算法

#include<iostream>
#include<functional>
#include<algorithm>
#include<vector>
#include<ctime>

using namespace std;

void fun(int i)
{
	cout << i << "\t";
}

void STLLuanXu()
{
	vector<int> vec;
	int n;
	while(cin >> n)
	{
		vec.push_back(n);
	}

	for_each(vec.begin(),vec.end(),fun);
	cout <<endl<<"-------------------------------------------------"<< endl;
	//亂序演算法 
	//srand種子要加上#include<ctime>標頭檔案 
	srand((unsigned int)time(0));
	random_shuffle(vec.begin(),vec.end());
	for_each(vec.begin(),vec.end(),fun);
	/////////////////////////////////////pai xu hou
	cout <<endl<<"-------------------------------------------------"<< endl;
	sort(vec.begin(),vec.end(),greater<int>());
	for_each(vec.begin(),vec.end(),fun);
	
}

int main()
{
	STLLuanXu();
	
	return 0;
}