std::find_end 用法

描述:
       在容器中寻找子序列最后出现的位置。
       用 operator== 比较元素。

定义:

template< class ForwardIt1, class ForwardIt2 >
ForwardIt1 find_end( ForwardIt1 first, ForwardIt1 last,
                     ForwardIt2 s_first, ForwardIt2 s_last );

template< class ForwardIt1, class ForwardIt2 >
constexpr ForwardIt1 find_end( ForwardIt1 first, ForwardIt1 last,
                               ForwardIt2 s_first, ForwardIt2 s_last );
template< class ForwardIt1, class ForwardIt2, class BinaryPredicate >
ForwardIt1 find_end( ForwardIt1 first, ForwardIt1 last,
                     ForwardIt2 s_first, ForwardIt2 s_last, BinaryPredicate p );

template< class ForwardIt1, class ForwardIt2, class BinaryPredicate >
constexpr ForwardIt1 find_end( ForwardIt1 first, ForwardIt1 last,
                               ForwardIt2 s_first, ForwardIt2 s_last, BinaryPredicate p );

可能的实现:

template<class ForwardIt1, class ForwardIt2>
ForwardIt1 find_end(ForwardIt1 first, ForwardIt1 last,
                    ForwardIt2 s_first, ForwardIt2 s_last)
{
    if (s_first == s_last)
        return last;
    ForwardIt1 result = last;
    while (true) {
        ForwardIt1 new_result = std::search(first, last, s_first, s_last);
        if (new_result == last) {
            break;
        } else {
            result = new_result;
            first = result;
            ++first;
        }
    }
    return result;
}
template<class ForwardIt1, class ForwardIt2, class BinaryPredicate>
ForwardIt1 find_end(ForwardIt1 first, ForwardIt1 last,
                    ForwardIt2 s_first, ForwardIt2 s_last,
                    BinaryPredicate p)
{
    if (s_first == s_last)
        return last;
    ForwardIt1 result = last;
    while (true) {
        ForwardIt1 new_result = std::search(first, last, s_first, s_last, p);
        if (new_result == last) {
            break;
        } else {
            result = new_result;
            first = result;
            ++first;
        }
    }
    return result;
}

参数:
       first, last - 要检验的元素范围
       s_first, s_last - 要搜索的元素范围
       p - 若元素应被当做相等则返回 ​true 的二元谓词。

返回值:
       返回范围 [first, last) 中 [s_first, s_last) 最后一次出现的开端的迭代器。若找不到这种序列,则返回 last。

示例:

#include <iostream>
#include <algorithm>
#include <string>

int main()
{
	std::string s = "abcdefgabcdefg";

	std::string s2 = "def";
	std::string s3 = "deg";

	auto iter1 = std::find_end(std::begin(s), std::end(s), std::begin(s2), std::end(s2));
	if (iter1 == std::end(s))
		std::cout << "s中不存在s2子序列" << std::endl;
	else
	{
		int nIndex = std::distance(std::begin(s), iter1);
		std::cout << "s2子序列在s中的索引位置:" << nIndex << std::endl;
	}
	//s2子序列在s中的索引位置:10

	auto iter2 = std::find_end(std::begin(s), std::end(s), std::begin(s3), std::end(s3));
	if (iter2 == std::end(s))
		std::cout << "s中不存在s3子序列" << std::endl;
	else
	{
		int nIndex = std::distance(std::begin(s), iter2);
		std::cout << "s3子序列在s中的索引位置:" << nIndex << std::endl;
	}
	//s中不存在s3子序列

	return 0;
}

#include <iostream>
#include <algorithm>
#include <vector>

struct SData
{
	int m_nData;
	double m_dData;

	SData(int nData, double dData)
		:m_nData(nData),m_dData(dData)
	{
	}
};

int main()
{
	std::vector<SData> v, v2;
	for (int i = 0; i < 5; i++)
	{
		SData data(i,i);
		v.push_back(data);
	}
	for (int i = 0; i < 5; i++)
	{
		SData data(i, i);
		v.push_back(data);
	}
	SData data(2,2.0);
	v2.push_back(data);

	//使用lambda表达式指定条件(一般用于自定义元素)
	auto iter = std::find_end(std::begin(v), std::end(v), std::begin(v2), std::end(v2), [](const SData& data1,const SData& data2)
	{
		return data1.m_nData == data2.m_nData;
	});
	if (iter == std::end(v))
		std::cout << "v中不存在v2子序列" << std::endl;
	else
	{
		int nIndex = std::distance(std::begin(v), iter);
		std::cout << "v2子序列在v中的索引位置:" << nIndex << std::endl;
	}
	//v2子序列在v中的索引位置:7

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值