常用的排序算法
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
void print(int val)
{
cout << val << " ";
}
void test01()
{
vector<int> v;
v.push_back(20);
v.push_back(45);
v.push_back(12);
v.push_back(57);
v.push_back(23);
//升序排序
sort(v.begin(), v.end());
for_each(v.begin(), v.end(), print);
cout << endl;
//降序排序
sort(v.begin(), v.end(), greater<int>());
for_each(v.begin(), v.end(), print);
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
# include<ctime>
void print(int val)
{
cout << val << " ";
}
void test01()
{
srand((unsigned int)time(NULL));
vector<int> v;
v.push_back(20);
v.push_back(45);
v.push_back(12);
v.push_back(57);
v.push_back(23);
//升序排序
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), print);
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
//总结:使用时,记得加随数机种子
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void print(int val)
{
cout << val<<" ";
}
void test01()
{
vector<int> v1;
vector<int> v2;
for (int i = 0; i <= 10; i++)
{
v1.push_back(i);
v2.push_back(i * 10);
}
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(), print);
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void print(int val)
{
cout << val << " ";
}
void test01()
{
vector<int> v1;
v1.push_back(34);
v1.push_back(23);
v1.push_back(567);
v1.push_back(367);
v1.push_back(75);
cout <<"反转前" <<endl;
for_each(v1.begin(), v1.end(), print);
cout << endl;
cout << "反转后" << endl;
reverse(v1.begin(), v1.end());
for_each(v1.begin(), v1.end(), print);
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
本文作者: 永生
本文链接: https://www.yys.zone/detail/?id=99
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
评论列表 (0 条评论)