有一个标准的算法来复制? [英] Is There a Standard Algorithm to Copy Until?
问题描述
我使用 istream_iterator< char>它
,所以我不能反向范围的迭代(或迭代它两次,没有大量的hastle。)
I am using an istream_iterator<char> it
, so I cannot iterate over the range in reverse (or iterate over it twice, without a great deal of hastle.)
以复制,直到满足条件。在标准库中有这样的工作:
I want to copy until a condition is met. Is there something that would work like this in the standard library:
copy_until(it, istream_iterator<char>(), ostream_iterator<char>(cout), [](const unsigned char i){ return isalpha(i); })
<
If I have to roll something I can I was just hoping for some magic that I haven't been able to figure out.
编辑:
我希望从我的 copy_until
函数中获得的行为是:
The behavior I would expect from my made up copy_until
function is:
while(it != istream_iterator<char>()) {
if(!isalpha(static_cast<unsigned char>(*it))) break;
cout << *it++;
}
推荐答案
http://en.cppreference.com/w/cpp/algorithm 提供对所有算法的非常有用的参考(不仅在算法库中,而且在 ,内存和 CStd库中提供)。 )以下是复制算法,即它们将Input Iterator,Output Iterator和lambda作为参数:
http://en.cppreference.com/w/cpp/algorithm Provides a very helpful reference to all the algorithms available in C++ (not just those in the Algorithm Library but also the Numeric, Memory, and CStd Libraries.) Of those the following are copying algorithms, that is they take Input Iterator(s), Output Iterator(s), and lambda(s) as arguments:
-
copy_if
复制范围中的元素,由[第一
,最后
pred
返回真 -
transform
将给定的函数应用于范围并将结果存储在另一个范围中 li>
-
remove_copy_if
从范围[第一
,,最后
d_first
,忽略符合特定条件的元素 -
replace_copy_if
复制范围内的所有元素[code>,
,
最后
)替换为d_first
$ c> new_value -
unique_copy
从范围[第一个
,最后
)到从d_first
开始的另一个范围,使得没有连续的相等元素...使用给定二进制谓词p
-
partition_copy
复制范围内的元素[第一
,最后
)到两个不同的范围,取决于谓词p
返回的值。满足谓词p
的元素将复制到d_first_true
开始的范围。其余元素将复制到d_first_false
-
merge
-
set_difference
-
a href =http://en.cppreference.com/w/cpp/algorithm/set_intersection =nofollow noreferrer>需要2个和输入范围set_intersection
-
set_symmetric_difference
-
set_union
-
adjacent_difference
计算范围的每个相邻元素对的第二个和第一个元素之间的差异[第一,
最后
)...使用给定的二进制函数/ code>
-
partial_sum
计算范围的子范围中的元素的部分和[第一
,last
),并将它们写入从d_first
开始的范围...要总结元素,第二个版本使用给定的二进制函数op
。 -
exclusive_scan
使用binary_op
[第一
,最后
) -
inclusive_scan
计算包含前缀和操作使用,
,
,最后
) li>
transform_exclusive_scan
使用<$ c $转换范围内的每个元素[第一,
,然后使用最后
c> unary_opbinary_op
对结果范围
- a href =http://en.cppreference.com/w/cpp/algorithm/transform_inclusive_scan =nofollow noreferrer>
transform_inclusive_scan
将每个unary_op
范围内的,然后使用
binary_op
在结果范围
copy_if
"Copies the elements in the range, defined by [first
,last
)... Only copies the elements for which the predicatepred
returns true"transform
"Applies the given function to a range and stores the result in another range."remove_copy_if
"Copies elements from the range [first
,last
), to another range beginning atd_first
, omitting the elements which satisfy specific criteria"replace_copy_if
"Copies the all elements from the range [first
,last
) to another range beginning atd_first
replacing all elements satisfying specific criteria withnew_value
"unique_copy
"Copies the elements from the range [first
,last
), to another range beginning atd_first
in such a way that there are no consecutive equal elements... Elements are compared using the given binary predicatep
"partition_copy
"Copies the elements from the range [first
,last
) to two different ranges depending on the value returned by the predicatep
. The elements, that satisfy the predicatep
, are copied to the range beginning atd_first_true
. The rest of the elements are copied to the range beginning atd_first_false
"Requires a 2nd input rangemerge
Requires a 2nd input rangeset_difference
Requires a 2nd input rangeset_intersection
Requires a 2nd input rangeset_symmetric_difference
Requires a 2nd input rangeset_union
adjacent_difference
"Computes the differences between the second and the first of each adjacent pair of elements of the range [first
,last
)... Differences are calculated using the given binary functionop
"partial_sum
"Computes the partial sums of the elements in the subranges of the range [first
,last
) and writes them to the range beginning atd_first
... To sum up the elements, the second version uses the given binary functionop
."exclusive_scan
"Computes an exclusive prefix sum operation usingbinary_op
for the range [first
,last
)"inclusive_scan
"Computes an inclusive prefix sum operation usingbinary_op
for the range [first
,last
)"transform_exclusive_scan
"Transforms each element in the range [first
,last
) withunary_op
, then computes an exclusive prefix sum operation usingbinary_op
over the resulting range"transform_inclusive_scan
"Transforms each element in the range [first
,last
) withunary_op
, then computes an inclusive prefix sum operation usingbinary_op
over the resulting range"
因为lambda仅用于修改范围[第一
,最后
)的1:1赋值为 d_first
; transform
, replace_copy_if
,并且所有的数值库算法都没有帮助$ c> adjacent_difference , partial_sum
, exclusive_scan
, inclusive_scan
, transform_exclusive_scan
和 transform_inclusive_scan
。)
Because the lambda is only used to modify the 1:1 assignment of the range [first
, last
) to d_first
; transform
, replace_copy_if
, and all of the Numeric Library algorithms are unhelpful (adjacent_difference
, partial_sum
, exclusive_scan
, inclusive_scan
, transform_exclusive_scan
, and transform_inclusive_scan
.)
- 如果在lambda条件满足后,范围的其余部分[
it
,istream_iterator& char>()
)被直接处理到一个2 和输出迭代器,partition_copy
/ strong> - 如果在lambda条件满足后,范围的其余部分[
it
,istream_iterator< char>()
)被一个函数迭代,这个函数可以在copy_if
(或remove_copy_if
或unique_copy
) - 在一般情况下,你的问题的答案是标准算法不提供copy_until,所以你需要使用
,而
-loop
- If after the lambda condition was met the remainder of the range [
it
,istream_iterator<char>()
) was to be directly copped to a 2nd output iterator,partition_copy
would solve your problem - If after the lambda condition was met the remainder of the range [
it
,istream_iterator<char>()
) was to be iterated over by a function, this function could be called on each value after the condition was met bycopy_if
(orremove_copy_if
orunique_copy
) - But in the general case, the answer to your question is the standard algorithms do not provide a "copy_until" so you'll need to use your
while
-loop
这篇关于有一个标准的算法来复制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!