开发者

copy n symbols to container [duplicate]

开发者 https://www.devze.com 2023-01-27 02:13 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: How to read arbitrary number of values using std::copy?
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

How to read arbitrary number of values using std::copy?

Hello. I'm reading nums from file. I do it with std::copy

copy(istream_iterator<int>(input_file), 
       istream_iterator<int>(), 
       back_inserter(first));

But this function copy only whole file, although I want to copy开发者_运维技巧 only N symbols. Thanks.


SGI's STL reference is quite good. The second argument needs to be the number of items you want to read.

Update

(After changing your initial post, which used copy_n)

You can use a simple loop to do what you want, as in

template <typename Input, typename Output>
Output copy_n (Input i, size_t N, Output o)
  {
    for (; N > 0; --N)
      {
        *o = *i;
        ++i;
        ++o;
      }
    return o;
  }
0

精彩评论

暂无评论...
验证码 换一张
取 消