sort
sort(iterator beg,iterator end, _func);
title:Important
重要排序算法
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void print02(int val)
{
cout << val << " ";
}
void print(vector<int>& v)
{
for(vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
vector<int>v1;
for(int i = 10; i > 0; i++)
{
v1.push_back(i);
}
print(v1);
sort(v1.begin(), v1.end());
for_each(v1.begin(), v1.end(), print02);
sort(v1.begin(), v1.end(), greater<int>());
for_each(v1.begin(), v1.end(), print02);
return 0;
}
/*output:
10 9 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1
*/
random_shuffle
random_shuffle(iterator beg, iterator end);
title:Important
洗牌算法
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void print02(int val)
{
cout << val << " ";
}
void print(vector<int>& v)
{
for(vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
vector<int>v1;
for(int i = 10; i > 0; i++)
{
v1.push_back(i);
}
print(v1);
sort(v1.begin(), v1.end());
for_each(v1.begin(), v1.end(), print02);
sort(v1.begin(), v1.end(), greater<int>());
for_each(v1.begin(), v1.end(), print02);
return 0;
}
/*output:
10 9 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1
*/
merge