C++ find()排序函数用法详解(深入了解,一文学会)

        find() 函数本质上是一个模板函数,用于在指定范围内查找和目标元素值相等的第一个元素。

C++ find()排序函数用法详解(深入了解,一文学会)目录

1 find()函数用法

2 find_if() 函数

3 find_if_not() 函数

4 find_end()函数

5 find_first_of() 函数

6 adjacent_find()函数


    本文作者原创,转载请附上文章出处与本文链接。 


1 find()函数用法

    InputIterator find (InputIterator first, InputIterator last, const T& val);

first 和 last 为输入迭代器,[first, last) 用于指定该函数的查找范围;val 为要查找的目标元素。

    正因为 first 和 last 的类型为输入迭代器,因此该函数适用于所有的序列式容器。

另外,该函数会返回一个输入迭代器,当 find() 函数查找成功时,其指向的是在 [first, last) 区域内查找到的第一个目标元素;如果查找失败,则该迭代器的指向和 last 相同。

值得一提的是,find() 函数的底层实现,其实就是用==运算符将 val 和 [first, last) 区域内的元素逐个进行比对。这也就意味着,[first, last) 区域内的元素必须支持==运算符。

#include <iostream>     // std::cout
#include <algorithm>    // std::find
#include <vector>       // std::vector
 
using namespace std;
 
int main() {
	//find() 函数作用于普通数组
	char stl[] = "https://blog.csdn.net/qq_37529913?spm=1011.2124.3001.5343";
	//调用 find() 查找第一个字符 'c'
	char * p = find(stl, stl + strlen(stl), 'c');
	//判断是否查找成功
	if (p != stl + strlen(stl)) {
		cout << p << endl;
	}
	//find() 函数作用于容器
	std::vector<int> myvector{ 10,20,30,40,50 };
	std::vector<int>::iterator it;
	it = find(myvector.begin(), myvector.end(), 30);
	if (it != myvector.end())
		cout << "查找成功:" << *it;
	else
		cout << "查找失败";
	return 0;
}

 

2 find_if() 函数

// 分数
std::vector<int> score{ 10, 20, 30, 40 };

// 找首个出现的,且大于30的元素
auto it = std::find_if(score.begin(), score.end(), [](const int &item) {return 30 < item; });

if (score.end() != it)
	std::cout << "找到了大于30的元素: " << *it << std::endl;
else
	std::cout << "没有找到大于30的元素\n";

3 find_if_not() 函数

// 分数
std::vector<int> score{ 10, 20, 30, 40 };

// 找首个出现的,且不大于30的元素
auto it = std::find_if_not(score.begin(), score.end(), [](const int &item) {return 30 < item; });

if (score.end() != it)
	std::cout << "找到了首个不大于30的元素: " << *it << std::endl;
else
	std::cout << "没有找到不大于30的元素\n";

4 find_end()函数

#include <iostream>     // std::cout
#include <algorithm>    // std::find_end
#include <vector>       // std::vector
using namespace std;
//以普通函数的形式定义一个匹配规则
bool mycomp1(int i, int j) {
    return (i%j == 0);
}

//以函数对象的形式定义一个匹配规则
class mycomp2 {
public:
    bool operator()(const int& i, const int& j) {
        return (i%j == 0);
    }
};

int main() {
    vector<int> myvector{ 1,2,3,4,8,12,18,1,2,3 };
    int myarr[] = { 1,2,3 };
    //调用第一种语法格式
    vector<int>::iterator it = find_end(myvector.begin(), myvector.end(), myarr, myarr + 3);
    if (it != myvector.end()) {
        cout << "最后一个{1,2,3}的起始位置为:" << it - myvector.begin() << ",*it = " << *it << endl;
    }

    int myarr2[] = { 2,4,6 };
    //调用第二种语法格式
    it = find_end(myvector.begin(), myvector.end(), myarr2, myarr2 + 3, mycomp2());
    if (it != myvector.end()) {
        cout << "最后一个{2,3,4}的起始位置为:" << it - myvector.begin() << ",*it = " << *it;
    }
    return 0;
}

程序执行结果为:

匹配{1,2,3}的起始位置为:7,*it = 1
匹配{2,3,4}的起始位置为:4,*it = 8

上面程序中共调用了 2 次 find_end() 函数:

  1. 第 22 行代码:调用了第一种语法格式的 find_end() 函数,其功能是在 myvector 容器中查找和 {1,2,3} 相等的最后一个子序列,显然最后一个 {1,2,3} 中元素 1 的位置下标为 7(myvector 容器下标从 0 开始);
  2. 第 29 行代码:调用了第二种格式的 find_end() 函数,其匹配规则为 mycomp2,即在 myvector 容器中找到最后一个子序列,该序列中的元素能分别被 {2、4、6} 中的元素整除。显然,myvector 容器中 {4,8,12} 和 {8,12,18} 都符合,该函数会找到后者并返回一个指向元素 8 的迭代器。

注意,find_end() 函数的第一种语法格式,其底层是借助 == 运算符实现的。这意味着,如果 [first1, last1] 和 [first2, last2] 区域内的元素为自定义的类对象或结构体变量时,使用该函数之前需要对 == 运算符进行重载。

5 find_first_of() 函数

不介绍使用迭代器可以替代

6 adjacent_find()函数

不介绍使用迭代器可以替代

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

双子座断点

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值