• 头文件:#include <algorithm>
  • sort:数列排序=============is_sorted:是否按规则排序
	std::vector<int> vecArray = { 4, 1, 6, 9, 3, 1, 4 };
	std::sort(vecArray.begin(), vecArray.end());//1, 1, 3, 4, 4, 6, 9 默认升序
	std::sort(vecArray.begin(), vecArray.end(), [](int x, int y){ return (x > y); });//9, 6, 4, 4, 3, 1, 1降序
	//是否按规则进行了排序is_sorted
	bool bSort = std::is_sorted(vecArray.begin(), vecArray.end(), [](int x, int y){ return (x > y); });
  • find在数列中查找要求的第1个元素============find_if:在数列中查找符合规则的第1个元素=================find_if_not:在数列中查找不符合规则的第一个元素
	std::vector<int> vecArray = { 4, 1, 6, 9, 3, 1, 4 };
	auto iter = std::find(vecArray.begin(), vecArray.end(), 9);//查找---元素
	if (iter != vecArray.end()) printf("find element:%d\n", *iter);//9
	//find_if 
	auto iter_if = std::find_if(vecArray.begin(), vecArray.end(), [](int x){ return (x == 9); });//查找---表达式
	if (iter_if != vecArray.end()) printf("find_if element:%d\n", *iter_if);//9
	//find_if_not
	auto iter_if_not = std::find_if_not(vecArray.begin(), vecArray.end(), [](int x){ return ((x % 2) != 0); });
	if (iter_if_not != vecArray.end()) printf("find_if_not element:%d \n", *iter_if_not);//4
  • search:在主序列中查找第1个出现的子序列
	std::vector<int> vecParent = {1, 4, 4, 9, 3, 4, 5};
	std::vector<int> vecChild = { 4, 9, 3 };
	//方式1=======在vecParent查找第一个vecChild子序列
	auto iter_search = std::search(vecParent.begin(), vecParent.end(), vecChild.begin(), vecChild.end());
	if (iter_search != vecParent.end())
		printf("search position:%d value:%d\n", iter_search - vecParent.begin(), vecParent[iter_search - vecParent.begin()]);//4
	//方式2=======在vecParent查找第一个能分别整除vecChild元素的子序列
	auto iter_search_new = std::search(vecParent.begin(), vecParent.end(), vecChild.begin(), vecChild.end(), [](int x, int y){ return ((x % y) == 0); });
	if (iter_search_new != vecParent.end()) 
		printf("search_new position:%d value:%d\n", iter_search_new - vecParent.begin(), vecParent[iter_search_new - vecParent.begin()]);//4
  • equal:两个数列(或字符串)是否相等
	std::vector<int> vecParent = { 1, 4, 4, 9, 3, 4, 5 };
	std::vector<int> vecChild = { 4, 9, 3 };
	bool bEqual = std::equal(vecParent.begin(), vecParent.end(), vecChild.begin());//false 两个序列是否相等

	const std::string strSrc = "test";
	const std::string strDst = "test";
	bool bEqual2 = std::equal(strSrc.begin(), strSrc.end(), strDst.begin());//true
  • any_of:是否含有某个(类)元素==============all_of:是否都是某个(类)元素================none_of:是否不包含某个(类)元素
	const std::string strData = "fesd2fSlkiK";
	bool bResult = any_of(strData.begin(), strData.end(), ::isupper);//true是否包含大写
	bool bResult2 = all_of(strData.begin(), strData.end(), ::islower);//false是否全部是小写
	bool bResult3 = none_of(strData.begin(), strData.end(), ::isalpha);//false是否不包含字母
	bool bResult4 = any_of(strData.begin(), strData.end(), ::isalnum);//true是否含有数字或字母
	bool bResult5 = any_of(strData.begin(), strData.end(), ::isdigit);//true是否含十进制数
  • count:在数列中查找相同元素的个数==================count_if:在数列中查找符合条件的元素的个数
	std::vector<int> vecSerial = {2, 4, 5, 7, 8, 9, 10, 4, 7, 7};
	int iNum = std::count(vecSerial.begin(), vecSerial.end(), 4);//2 在序列中查找元素4的个数
	int iNum2 = std::count(vecSerial.begin(), vecSerial.end(), 7);//3 在序列中查找元素7的个数
	int iNum3 = std::count_if(vecSerial.begin(), vecSerial.end(), [](int i) { return ((i % 2) != 0); });//5 在序列中查找奇数个数

 

本文地址:https://blog.csdn.net/zhoumin4576/article/details/110136344