如何创建copy_if算法? [英] How to create copy_if algorithm?
问题描述
在STL中,没有copy_if算法。但是我怎么能模仿呢?
我正在考虑使用find_if然后复制。
但是如何通过InputIterator(开始和结束)来复制以便它
知道它试图复制的范围。
感谢您的任何想法。
In STL, there is no copy_if algorithm. But how can I emulate that?
I am thinking about using find_if and then a copy.
But how can I pass the InputIterator (begin and end) to copy so that it
knows the range it tries to copy from.
Thanks for any idea.
推荐答案
si ********** *****@gmail.com 写道:
在STL中,没有copy_if算法。但我怎么能效仿呢?
它应该做什么?
我正在考虑使用find_if然后复制一份。
但是如何通过InputIterator(开始和复制,以便它知道它试图复制的范围。
In STL, there is no copy_if algorithm. But how can I emulate that?
What should it do?
I am thinking about using find_if and then a copy.
But how can I pass the InputIterator (begin and end) to copy so that it
knows the range it tries to copy from.
想想''复制''......
模板< class Iter,类Iter2类Func>
void copy_if(Iter beg,Iter end,Iter2 beg2,Func f)
{
while(beg!= end){
if(f(* beg))
* beg2 ++ = * beg;
求++;
}
}
这里,''beg2''只有在分配了某些东西时才会增长。如果您想要不同的
行为,请修改代码。
V
-
请从邮寄回复时我的地址删除资金
Think of ''copy''...
template<class Iter, class Iter2 class Func>
void copy_if(Iter beg, Iter end, Iter2 beg2, Func f)
{
while (beg != end) {
if (f(*beg))
*beg2++ = *beg;
beg++;
}
}
Here, ''beg2'' only grows when something is assigned. If you want different
behaviour, modify the code.
V
--
Please remove capital As from my address when replying by mail
si *************** @ gmail.com 写道:
在STL中,没有copy_if算法。但是我怎么能模仿呢?
我正在考虑使用find_if然后使用副本。
但是如何通过InputIterator(开始和结束)进行复制以便它知道范围它试图从中复制。
In STL, there is no copy_if algorithm. But how can I emulate that?
I am thinking about using find_if and then a copy.
But how can I pass the InputIterator (begin and end) to copy so that it
knows the range it tries to copy from.
如果出现错误,您希望copy_if做什么?忽略输入和输出上的
元素,或仅输入?即,如果失败,
你在下一次尝试之前增加输出迭代器吗?
Ben Pope
-
我不只是一个数字。对于很多人来说,我被称为字符串...
What would you like copy_if to do in case of a false? Ignore that
element on the input and output, or just the input? I.e., if it fails,
do you increment the output iterator before the next attempt?
Ben Pope
--
I''m not just a number. To many, I''m known as a string...
在假的情况下,只是不要复制(忽略
上的那个元素)
输入)。我认为输出迭代器只会在每次我们增加输出时添加一些内容时递增。所以在假的情况下,我们忽略了
输入元素,不要将它添加到输出中,因此输出迭代器
应该保持不变。 (我想)。
谢谢。
In the case of a false, just do not copy (ignore that element on the
input). And i think the output iterator only increment everytime we
add something to the output. So in the case of a false, we ignore the
input element, do not add that to the output, hence the output iterator
should be unchanged. (I think).
Thank you.
这篇关于如何创建copy_if算法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!