Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As was noted by <a href="https://stackoverflow.com/questions/3829885/how-to-copy-a-certain-number-of-chars-from-a-file-to-a-vector-the-stl-way/3830107#3830107">Steve</a>, this would need <a href="http://www.sgi.com/tech/stl/copy_n.html" rel="nofollow noreferrer"><code>copy_n()</code></a>, which, due to an oversight, isn't in the current standard library, but will be in C++1x. You can implement one yourself easily, here's one I believe to be correct: </p> <pre><code>template&lt;class InIt, class OutIt&gt; OutIt copy_n(InIt src, OutIt dest, size_t n) { if (!n) return dest; *dest = *src; while (--n) *++dest = *++src; return ++dest; } </code></pre> <p>Note that <code>std::copy_n()</code> presumes the input iterator to be able to deliver <code>n</code> objects. When reading from a file, this could be problematic. </p> <hr> <p>Absent of <code>std::copy_n()</code>, you could use <code>std::generate_n</code>.</p> <pre><code>template&lt; typename InIt &gt; struct input_generator { typedef std::iterator_traits&lt;InIt&gt;::value_type value_type; input_generator(InIt begin, InIt end) begin_(begin), end_(end) {} value_type operator()() { assert(it_ != end); return *it_++; } Init begin_; Init end_; }; std::vector&lt;char&gt; buffer; buffer.reserve(42); std::generate_n( std::back_inserter(buffer) , 42 , input_generator(std::istream_iterator&lt;char&gt;(file)) , input_generator(std::istream_iterator&lt;char&gt;()) ); </code></pre> <hr> <p>However, I don't see this as an advantage over reading directly from the file as <a href="https://stackoverflow.com/questions/3829885/how-to-copy-a-certain-number-of-chars-from-a-file-to-a-vector-the-stl-way/3830354#3830354">avakar showed</a>. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload