我有一个关于使用输入和输出迭代器的练习题。功能的标题是如下输入和输出迭代器
template<class InpIter,class OutpIter>
OutpIter my_unique_copy(InpIter first, InpIter last, OutpIter result)
该函数应的元素从所述范围复制[第一,最后]导致。在连续的重复元素组中,只有第一个值被复制。返回值是元素复制到的范围的末尾。 复杂性:线性
我有一个想法,做刚琢磨了一点帮助,因为我不是迭代器舒适又不失
template<class InpIter,class OutpIter>
OutpIter my_unique_copy(InpIter first, InpIter last, OutpIter result){
InpIter current=first;
first++;//point to second element
while(first!=last){
while(*first==*current){//Keep comparing elements to current to see if they're same
first++;
}
result=current;
current=first;
result++;
first++;
}
return result;
}
好的,问题是什么? :) – jrok
编译器不会咬人......让自己感觉舒适的唯一方法就是试试看。 – Cogwheel
您也可以使用'unique_copy()' – Kunal