copy begrenzen



  • Folgendes Problem:

    Ich hab eine txt Datei in der verschiedene Werte liegen,
    und versuch diese mittels einem istream_iterator wieder auszulesen:

    vector<value> v;
    	container::size_type n;
    	i >> n;
    	v.reserve(n);
    	istream_iterator<int> begin(i); // Anfangsiterator auf die Datei
        istream_iterator<int> end;
    	std::copy(begin, end,std::back_inserter(cont));
    

    Ich würde jetzt gerne copy sagen, das es nach 100 elementen aufhören soll,
    zu kopieren, gibts da ne Möglichkeit ?
    Oder müsste ich eine schleife machen (for(int i=0;...) ?

    Devil



  • Description

    The copy() algorithm copies values from the range specified by [start, finish) to the range specified by [result, result + (finish - start)). copy() returns result + (finish - start). For each non-negative integer n < (finish - start), copy() assigns *(start + n) to *(result + n). The result of copy() is undefined if result is in the range [start, finish).

    Unless result is an insert iterator, copy() assumes that all iterators in the range [result + (finish - start)] are dereferenceable.

    The copy_backward() algorithm copies elements in the range specified by [start, finish) into the range specified by [result - (finish - start), result), starting from the end of the sequence (finish-1) and progressing to the front (start). Note that copy_backward() does not reverse the order of the elements, it simply reverses the order of transfer. copy_backward() returns result - (finish - start). You should use copy_backward() instead of copy() when finish is in the range [result - (finish - start), result). For each positive integer n <= (finish - start), copy_backward() assigns *(finish - n) to *(result - n). The result of copy_backward() is undefined if result is in the range [start, finish). copy_backward() should be used when finish is in the range [result - (finish - start), result).

    Unless result is an insert iterator, copy_backward() assumes that all iterators in the range [result - (finish - start), result) are dereferenceable.

    quelle



  • entweder boost::copy_n oder

    template <class InputIter, class Size, class OutputIter>
    OutputIter copy_n(InputIter first, Size count,
                      OutputIter result) {
      for ( ; count > 0; --count) {
        *result = *first;
        ++first;
        ++result;
      }
      return result;
    }
    


  • Hm, also ein eigenes Copy, naja, jetzt funktionierts 😃

    Devil



  • devil81 schrieb:

    Hm, also ein eigenes Copy, naja, jetzt funktionierts 😃

    Devil

    Du musst das natuerlich nicht selbst schreiben, copy_n gibt es bereits in
    algorithms ;).

    mfg
    v R



  • virtuell Realisticer schrieb:

    Du musst das natuerlich nicht selbst schreiben, copy_n gibt es bereits in
    algorithms 😉

    🙄

    This is an SGI/BOOST extension



  • virtuell Realisticer schrieb:

    devil81 schrieb:

    Hm, also ein eigenes Copy, naja, jetzt funktionierts 😃

    Devil

    Du musst das natuerlich nicht selbst schreiben, copy_n gibt es bereits in
    algorithms ;).

    mfg
    v R

    Nicht bei mir 😉


Anmelden zum Antworten