常用的排序算法
sort算法
void test()
{
vector<int>v;
v.push_back(10);
v.push_back(30);
v.push_back(40);
v.push_back(20);
v.push_back(50);
sort(v.begin(), v.end(),greater<int>());
for_each(v.begin(), v.end(), [](int val){cout << val << " "; });
cout << endl;
}
洗牌算法
void test()
{
srand((unsigned int)time(NULL));
vector<int>v;
for (int i = 0; i < 10;i++)
{
v.push_back(i);
}
for_each(v.begin(), v.end(), [](int val){cout << val << " "; });
cout << endl;
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), [](int val){cout << val << " "; });
cout << endl;
}
反转算法
void test()
{
vector<int>v;
v.push_back(10);
v.push_back(30);
v.push_back(40);
v.push_back(20);
v.push_back(50);
cout << "反转前遍历结果为: " << endl;
for_each(v.begin(), v.end(), [](int val){cout << val << " "; });
cout << endl;
reverse(v.begin(), v.end());
cout << "反转后遍历结果为: " << endl;
for_each(v.begin(), v.end(), [](int val){cout << val << " "; });
cout << endl;
}
合并算法
void test01()
{
vector<int>v1;
vector<int>v2;
for (int i = 0; i < 10;i++)
{
v1.push_back(i);
v2.push_back(i+1);
}
vector<int>vTarget;
vTarget.resize(v1.size() + v2.size());
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
for_each(vTarget.begin(), vTarget.end(), [](int val){cout << val << " "; });
cout << endl;
}