有一个标准的算法来复制? [英] Is There a Standard Algorithm to Copy Until?

查看:162
本文介绍了有一个标准的算法来复制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 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 需要2 nd 输入范围

  • set_difference 需要2个 nd 输入范围

  • a href =http://en.cppreference.com/w/cpp/algorithm/set_intersection =nofollow noreferrer> set_intersection 需要2个输入范围

  • set_symmetric_difference 需要2个 nd 输入范围

  • set_union 需要2个输入范围

  • adjacent_difference 计算范围的每个相邻元素对的第二个和第一个元素之间的差异[第一,最后)...使用给定的二进制函数 / code>

  • partial_sum 计算范围的子范围中的元素的部分和[第一 last ),并将它们写入从 d_first 开始的范围...要总结元素,第二个版本使用给定的二进制函数 op

  • exclusive_scan 使用 binary_op [第一最后

  • inclusive_scan 计算包含前缀和操作使用,最后) li>
  • transform_exclusive_scan 使用<$ c $转换范围内的每个元素[第一,最后 c> unary_op ,然后使用 binary_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 predicate pred 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 at d_first, omitting the elements which satisfy specific criteria"
  • replace_copy_if "Copies the all elements from the range [first, last) to another range beginning at d_first replacing all elements satisfying specific criteria with new_value"
  • unique_copy "Copies the elements from the range [first, last), to another range beginning at d_first in such a way that there are no consecutive equal elements... Elements are compared using the given binary predicate p"
  • partition_copy "Copies the elements from the range [first, last) to two different ranges depending on the value returned by the predicate p. The elements, that satisfy the predicate p, are copied to the range beginning at d_first_true. The rest of the elements are copied to the range beginning at d_first_false"
  • merge Requires a 2nd input range
  • set_difference Requires a 2nd input range
  • set_intersection Requires a 2nd input range
  • set_symmetric_difference Requires a 2nd input range
  • set_union Requires a 2nd input range
  • 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 function op"
  • partial_sum "Computes the partial sums of the elements in the subranges of the range [first, last) and writes them to the range beginning at d_first... To sum up the elements, the second version uses the given binary function op."
  • exclusive_scan "Computes an exclusive prefix sum operation using binary_op for the range [first, last)"
  • inclusive_scan "Computes an inclusive prefix sum operation using binary_op for the range [first, last)"
  • transform_exclusive_scan "Transforms each element in the range [first, last) with unary_op, then computes an exclusive prefix sum operation using binary_op over the resulting range"
  • transform_inclusive_scan "Transforms each element in the range [first, last) with unary_op, then computes an inclusive prefix sum operation using binary_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 by copy_if (or remove_copy_if or unique_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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆