VS2017程序下载:https://pan.baidu.com/s/11KR9Biaz1qsJuKp_0NT4JA?pwd=4prh
目录
1. sort排序
2 random_shuffle随机排序
3. merge 合并
4. reverse 反转
1. sort排序
功能描述:
对容器内元素进行排序,默认升序
函数原型:
// beg 开始迭代器 end 结束迭代器 _Pred 谓词
sort(iterator beg, iterator end, _Pred);
程序:
#include<iostream>
using namespace std;
#include <algorithm>
#include <vector>
#include <functional>void myPrint(int val)
{cout << val << " ";
}
void test01()
{vector<int>v;v.push_back(10);v.push_back(30);v.push_back(50);v.push_back(20);v.push_back(40);cout << " sort默认排序: ";//利用sort进行排序,默认升序sort(v.begin(), v.end());//容器元素进行升序排列for_each(v.begin(), v.end(), myPrint);//遍历容器,并打印cout << endl;cout << " sort调用greater降序: ";//改变为 降序,template<class T> bool greater<T> //大于,关系仿函数sort(v.begin(), v.end(), greater<int>());for_each(v.begin(), v.end(), myPrint);cout << endl;cout << " sort调用less升序: ";//改变为 升序,template<class T> bool less<T> //小于,关系仿函数sort(v.begin(), v.end(), less<int>());for_each(v.begin(), v.end(), myPrint);cout << endl;
}int main() {test01();system("pause");return 0;
}
运行结果:
2 random_shuffle随机排序
功能描述:
指定范围内的元素,随机调整次序(相当于随机洗牌)
函数原型:
// 指定范围内的元素,随机调整次序
// beg 开始迭代器 end 结束迭代器
random_shuffle(iterator beg, iterator end);
程序:
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <ctime>void myPrint(int val)//遍历打印数据
{cout << val << " ";
}void test01()
{//随机时间种子,保证每次运行结果不一样srand((unsigned int)time(NULL));vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);//尾插数据 0~9}cout << "打乱前数据: ";for_each(v.begin(), v.end(), myPrint);cout << endl;//利用洗牌 算法 打乱顺序random_shuffle(v.begin(), v.end());cout << "打乱后数据: ";for_each(v.begin(), v.end(), myPrint);cout << endl;
}int main() {test01();system("pause");return 0;
}
运行结果:
3. merge 合并
功能描述:
两个有序的容器元素合并,并存储到另一容器中
函数原型:
// 容器元素合并,并存储到另一容器中
// 注意: 两个容器必须是有序的// beg1 容器1开始迭代器 end1 容器1结束迭代器 beg2 容器2开始迭代器 end2 容器2结束迭代器
// dest 目标容器开始迭代器
merge(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
程序:
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>/*重载函数调用操作符的类,其对象常称为函数对象.函数对象使用重载的()时,
行为类似函数调用,也叫仿函数.函数对象(仿函数)是一个类,不是一个函数 */
//仿函数
class print02
{
public:void operator()(int val)//重载(){cout << val << " ";}
};//普通函数
void myPrint(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);}cout << " v1 原始数据: ";for_each(v1.begin(), v1.end(), print02());cout << endl;cout << " v2 原始数据: ";for_each(v2.begin(), v2.end(), print02());cout << endl;//目标容器vector<int>v3;//提前给目标容器分配空间v3.resize(v1.size() + v2.size());cout << " 合并v1,v2 数据,搬运给 v3 : ";//将容器 v1,v2的数据搬运到 vTarget,注意:容器必须有序merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());//遍历容器,并打印for_each(v3.begin(), v3.end(), myPrint);cout << endl;
}int main() {test01();system("pause");return 0;
}
运行结果:
4. reverse 反转
功能描述:
将容器内元素进行反转
函数原型:
// 反转指定范围的元素
// beg 开始迭代器 end 结束迭代器
reverse(iterator beg, iterator end); `
程序:
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>/*重载函数调用操作符的类,其对象常称为函数对象.函数对象使用重载的()时,
行为类似函数调用,也叫仿函数.函数对象(仿函数)是一个类,不是一个函数 */
//仿函数
class print02
{
public:void operator()(int val)//重载(){cout << val << " ";}
};//普通函数
void myPrint(int val)//遍历打印数据
{cout << val << " ";
}
void test01()
{vector<int>v;v.push_back(10);v.push_back(30);v.push_back(50);v.push_back(20);v.push_back(40);cout << "反转前数据: " << endl;//遍历容器,并打印for_each(v.begin(), v.end(), myPrint);cout << endl;cout << "反转后数据: " << endl;reverse(v.begin(), v.end());//反转数据for_each(v.begin(), v.end(), print02());//遍历容器,并打印cout << endl;
}int main() {test01();system("pause");return 0;
}
运行结果: