#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