STL中常用的算法的使用(一):非更易型算法

非更易型算法既不改动元素的次序,也不改动元素值。

//对于每个测试函数, 导入以下头文件
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

count( ):返回元素的个数

int main() {
	vector<int> arr = { 1,2,3,4,5,6,1,2,3,1,1};
	cout << count(arr.begin(),arr.end(),1)<<endl;    //返回arr从头到尾1的个数   4个
	cout << count(arr.begin()+1, arr.end(), 1);		//返回arr从1下标开始到尾1的个数    3个
}

在这里插入图片描述

count_if( ):返回某一准则(条件)的元素个数

bool test(int& i)
{
	return i % 4 == 0;
}

class Test {
public:
	Test(int _val) :val(_val) {}
	bool operator()(int& v) const {   //仿函数
		return v%val == 0;
	}

private:
	int val;
};

int main() {
	vector<int> arr = { 1,2,3,4,5,6,1,2,3,1,10};
	cout << count_if(arr.begin(), arr.end(), [](int i) {return i - 2 == 0; }) << endl;   //返回 减去2等于0的元素个数
	cout << count_if(arr.begin(), arr.end(), [](int i) {return i%3 == 0; }) << endl;     //返回 余3等于0的元素个数
	
	cout << count_if(arr.begin(), arr.end(), test) << endl; 							//返回 余4等于0的元素个数
	
	cout << count_if(arr.begin(), arr.end(), Test(5)) << endl;							//返回 余5等于0的元素个数	
}

在这里插入图片描述

min_element( ): 返回最小元素的位置

int main() {
	vector<int> arr = { 1,2,3,4,5,6,1,2,3,1,10,0};
	vector<char> arr_2 = { 'a','b','d','e'};
	cout << *(min_element(arr.begin(), arr.end())) << endl;      //*用于取迭代器对应的值
	cout << *(min_element(arr_2.begin(), arr_2.end())) << endl;  
}

在这里插入图片描述

max_element( ): 返回最大元素的位置

int main() {
	vector<int> arr = { 1,2,3,4,5,6,1,2,3,1,10,0};
	vector<char> arr_2 = { 'a','b','d','e'};
	cout << *(max_element(arr.begin(), arr.end())) << endl;   
	cout << *(max_element(arr_2.begin(), arr_2.end())) << endl;  
}

在这里插入图片描述

minmax_element:返回最小值和最大值位置组成的pair对象

int main() {
	vector<int> arr = { 1,2,3,4,5,6,1,2,3,1,10,0};
	vector<char> arr_2 = { 'a','b','d','e'};
	cout << *(minmax_element(arr.begin(), arr.end()).first) <<' '\   //最小值- first位置
		<< * (minmax_element(arr.begin(), arr.end()).second) << endl;		//最大值 - second位置
	cout << *(minmax_element(arr_2.begin(), arr_2.end()).first) <<' '\
		<< * (minmax_element(arr_2.begin(), arr_2.end()).second) << endl;
}

在这里插入图片描述

find( ):查找与被传入值相等的第一个元素的位置

int main() {
	vector<int> arr = { 1,2,3,4,5,6,1,2,3,1,10,0};
	vector<char> arr_2 = { 'a','b','d','e'};
	cout << (find(arr.begin(), arr.end(), 1) == arr.end())<< endl;   // 找到,布尔值为false,0;
	cout << distance(arr.begin(), find(arr.begin(), arr.end(), 1)) << endl;  //返回第一个元素位置,第一个1与数组头距离为0
	cout << (find(arr_2.begin(), arr_2.end(), 'x') == arr_2.end()) << endl;   //在arr_2中寻找‘x’,未找到则范围arr_2.end()位置
}

在这里插入图片描述

find_if( ):查找满足某个准则的第一个元素的位置

int main() {
	vector<int> arr = { 1,2,3,4,5,6,1,2,3,1,10,0};
	vector<char> arr_2 = { 'a','b','d','e'};
	//也可以使用一般函数,或者仿函数传值
	cout << (find_if(arr.begin(), arr.end(), [](int& i) { return i - 3 == 0; }) == arr.end()) << endl; //查找满足减去3等于0的第一个元素
	cout << distance(arr.begin(),find_if(arr.begin(), arr.end(), [](int& i) { return i - 3 == 0; })) << endl;
	cout << (find_if(arr_2.begin(), arr_2.end(), [](char& i) { return i=='x'; }) == arr_2.end()) << endl;
}

在这里插入图片描述

find_if_not( ):查找不满足某个准则的第一个元素的位置

int main() {
	vector<int> arr = { 1,2,3,4,5,6,1,2,3,1,10,0};
	vector<char> arr_2 = { 'a','b','d','e'};
	//能否找到一个不满足减去1等于0的数
	cout << (find_if_not(arr.begin(), arr.end(), [](int& i) { return i - 1 == 0; }) == arr.end()) << endl; 
	//找到第一个不满足减去1等于0的数和首部的距离
	cout << distance(arr.begin(), find_if_not(arr.begin(), arr.end(), [](int& i) { return i - 1 == 0; })) << endl;
	//能否找到一个不满足等于'x'的字符的位置
	cout << (find_if_not(arr_2.begin(), arr_2.end(), [](char& i) { return i=='x'; }) == arr_2.end()) << endl;
}

在这里插入图片描述

search_n( ):查找区间内连续n个满足某一条件或者等于某一值的情况,并返回满足条件的第一个元素位置

int main() {
	vector<int> arr = { 1,2,3,4,4,4,4,3,2,3,3,3,10};
	//返回连续3个4的满足条件位置
	auto it = search_n(arr.begin(),arr.end(),3,4);
	if(it!=arr.end())
		cout << distance(arr.begin(), it) << endl;
	//其中第四个参数对应着lambda表达式中的第二个参数,在这里我们没有实际作用。
	auto it1 = search_n(arr.begin(), arr.end(), 3, 0, [](int i, int) {return i >= 3; });
	if (it1 != arr.end())
		cout << distance(arr.begin(), it1) << endl;
	//和上面等价	
	auto it3 = search_n(arr.begin(), arr.end(), 3, 3, [](int i, int l) {return i >= l; });
	if (it3 != arr.end())
		cout << distance(arr.begin(), it3) << endl;
	
	//第一个连续三个小于5的开始位置
	auto it2 = search_n(arr.begin(), arr.end(), 3, 5, less<int>());
	if (it2 != arr.end())
		cout << distance(arr.begin(), it2) << endl;
}

在这里插入图片描述

search( ):查找某区间第一次出现的位置

int main() {
	string text{ "Smith, where Jones had had \"had\", had had \"had had\"."" \"Had had\" had had the examiners\' approval." };
	std::cout << text << std::endl;
	string phrase{ "had had" };
	auto iter = std::begin(text);
	auto end_iter = end(text);
	//toupper 把小写字母转换为大写字母。
	while ((iter = std::search(iter, end_iter, std::begin(phrase), std::end(phrase), \
	[](char ch1, char ch2) { return std::toupper(ch1) == std::toupper(ch2); })) != end_iter)
	{
		++count;
		std::advance(iter, phrase.size()); //将iter往后移动phrase.size()个距离。
	}
	std::cout << "\n\"" << phrase << "\" was found " << count << " times." << std::endl;
}

在这里插入图片描述

find_end( ):查找某个子区间最后一次出现的位置 (完整区间)

int main() {
	string text{ "Smith, where Jones had had \"had\", had had \"had had\"."" \"Had had\" had had the examiners\' approval." };
	string phrase{ "had had" };
	auto iter = begin(text);
	auto end_iter = end(text);
	//最后一次出现子区间的位置
	iter = find_end(iter, end_iter, begin(phrase), end(phrase), [](char ch1, char ch2) { return toupper(ch1) == toupper(ch2); });
	if (iter != end(text)) {
		cout << distance(text.begin(), iter)<<endl;  	//与头的距离
		cout << distance( iter, text.end());			//与尾的距离
	}
}

在这里插入图片描述

find_first_of( ):查找数个元素可能的第一个出现位置 (可能搜索不完整)

int main() {
	string text{ "Smith, where Jones had had \"had\", had had \"had had\"."" \"Had had\" had had the examiners\' approval." };
	string phrase{ "had had" };
	auto iter = begin(text);
	auto end_iter = end(text);
	iter = find_first_of(iter, end_iter, begin(phrase), end(phrase), [](char ch1, char ch2) { return toupper(ch1) == toupper(ch2); });
	if (iter != end(text)) {
		cout << distance(text.begin(), iter)<<endl;  //找到了第一个h  后面可能存在phrase
		cout << distance( iter, text.end());
	}
}

在这里插入图片描述

adjacent_find( ):查找连续满足条件的元素的位置

bool mycomp1(int i, int j) {
	return (i == j);
}
int main() {
	string text{ "Smith, where Jones had had \"had\", had had \"had had\"."" \"Had had\" had had the examiners\' approval." };
	string phrase{ "had had" };
	auto iter = begin(text);
	auto end_iter = end(text);
	iter = adjacent_find(iter, end_iter, mycomp1);  //查找连续两个相等元素的位置  pp
	if (iter != end(text)) {
		cout << distance(text.begin(), iter)<<endl;
		cout << distance( iter, text.end());
	}
}

在这里插入图片描述

equal( ):判断两个区间是否相等

int main() {
	vector<int> a{ 1,2,3 }, b{ 1,2,3,4 };
	cout << "以a为比较范围,判断a,b是否相同:" << equal(a.begin(), a.end(), b.begin()) << endl;  //长度以a决定,从b.begin()开始比较
	cout << "要求容器相同,判断a,b是否相同:" << equal(a.begin(), a.end(), b.begin(), b.end()) << endl;
}

在这里插入图片描述

is_sorted( ):返回区间内元素是否排序

int main() {
	vector<int> a{ 1,2,3 }, b{2,1,4 };
	cout << is_sorted(a.begin(), a.end()) << endl;
	cout << is_sorted(b.begin(), b.end()) << endl;
}

在这里插入图片描述

is_heap( ):返回区间内的元素是否形成一个heap

int main() {
	vector<int> a{ 9, 5, 4, 1, 2, 3 }, b{2,1,4 };
	cout << is_heap(a.begin(), a.end()) << endl;  //a是堆
	cout << is_heap(b.begin(), b.end()) << endl;	//b不是
}

在这里插入图片描述

all_of( ):返回是否所有的元素都符合某个元素

int main() {
	vector<int> a{ 9, 5, 4,  3, 2, 3, 1 }, b{2,1,4 };
	cout << all_of(a.begin(), a.end(), [](int i) {return i > 2; }) << endl;  //都大于2
	cout << all_of(b.begin(), b.end(), [](int i) {return i > 0; }) << endl; //都大于0
}

在这里插入图片描述

any_of( ):返回是否至少存在一个元素符合某个规则

int main() {
	vector<int> a{ 9, 5, 4,  3, 2, 3, 1 }, b{2,1,4 };
	cout << any_of(a.begin(), a.end(), [](int i) {return i > 2; }) << endl;  //存在大于2
	cout << any_of(b.begin(), b.end(), [](int i) {return i > 4; }) << endl; //存在大于4
}

在这里插入图片描述

none_of( ):返回是否无任何元素满足某个规则

int main() {
	vector<int> a{ 9, 5, 4,  3, 2, 3, 1 }, b{2,1,4 };
	cout << none_of(a.begin(), a.end(), [](int i) {return i > 2; }) << endl;
	cout << none_of(b.begin(), b.end(), [](int i) {return i > 4; }) << endl;  //没有大于4的
}

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值