简单的adajent_find_t copy_t count_1_t简单的实现

#include <iostream>
#include<memory>
#include<iterator>
using namespace std;
/*
这里利用了游标Iterator实现了adajent_find_1
实现的是在first - last之间的找出adajent and equal的元素
返回的是找到相等元素的第一的元素的地址
如果没有则返回last
*/
template<typenameForwardIterator first,ForwardIterator last)
{
		if(first!=last)
		{
			ForwardIterator next=first;
			while(++first!=last)
			{
				if(*first==*next)
				return next;
				next=first;
			}
		}
		return last;
}
/*
<template typename InputIterator,typename OutputIterator,Pred f>
void copy_t(inputIterator first,InputIterator last,Pred f)
{
<span style="white-space:pre">	</span>for(;first!=last;++first,++result)
{
                if(Pred(*first)
		*result=*first;
}
}这里的Pred为Unary prediction


/*
template<typename InputIterator,OutputIterator,typename Pred>
void copy_t(InputIterator first,InputIterator last,OutputIterator result,Pred f)

*/
template<typename InputIterator,typename OutputIterator>
void copy_t(InputIterator</span> first,InputIterator last,OutputIterator result)
{
	for(;first!=last;++first,++result)
		*result=*first;
}


/*

*/
template<typename InputIterator
size_t count_1_t(InputIterator first,InputIterator last,typename iterator_traits<InputIterator>::value_type& T)
{
	typename iterator_traits<InputIterator>::difference_type count(0);
	while(first!=last)
	{
		if(*first++==T)
		++count;
	}
	return count;
}

int main()
{
	int arr[]={ 1,2,3,4,4,5,7,8};
	int* cur_int = adajent_find_1(arr,arr+7);
	cout<<*cur_int<<endl;
	int arr_1[10];
	copy_t(arr,arr+8,arr_1);
	
	for(auto& i:arr_1)
	{
		cout<<i<<endl;
	}
	int i = 4;
	auto& t = i;
  auto count=count_1_t(arr,arr+7,t);
  cout<<count<<endl;
   return 0;
	
}



 
实现了从copy first-last to result...
同样可以这样
上面利用了C++11和C++14





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值