Namespaces
Variants

std:: copy_n

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
copy_n
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
Definiert in Header <algorithm>
template < class InputIt, class Size, class OutputIt >
OutputIt copy_n ( InputIt first, Size count, OutputIt result ) ;
(1) (seit C++11)
(constexpr seit C++20)
template < class ExecutionPolicy,

class ForwardIt1, class Size, class ForwardIt2 >
ForwardIt2 copy_n ( ExecutionPolicy && policy,

ForwardIt1 first, Size count, ForwardIt2 result ) ;
(2) (seit C++17)
1) Kopiert genau count Werte aus dem Bereich beginnend bei first in den Bereich beginnend bei result . Formal wird für jede ganze Zahl i in [ 0 , count ) die Operation * ( result + i ) = * ( first + i ) ausgeführt.
Die Überlappung von Bereichen ist formal erlaubt, führt jedoch zu unvorhersehbarer Reihenfolge der Ergebnisse.
2) Gleich wie (1) , aber ausgeführt gemäß policy .
Diese Überladung nimmt nur dann an der Überladungsauflösung teil, wenn alle folgenden Bedingungen erfüllt sind:

std:: is_execution_policy_v < std:: decay_t < ExecutionPolicy >> ist true .

(bis C++20)

std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> ist true .

(seit C++20)

Inhaltsverzeichnis

Parameter

first - der Anfang des Bereichs der zu kopierenden Elemente
count - Anzahl der zu kopierenden Elemente
result - der Anfang des Zielbereichs
policy - die execution policy die verwendet werden soll
Typanforderungen
-
InputIt muss die Anforderungen von LegacyInputIterator erfüllen.
-
OutputIt muss die Anforderungen von LegacyOutputIterator erfüllen.
-
ForwardIt1, ForwardIt2 muss die Anforderungen von LegacyForwardIterator erfüllen.

Rückgabewert

Iterator im Zielbereich, der über das letzte kopierte Element hinaus zeigt, wenn count > 0 oder result andernfalls.

Komplexität

Keine Zuweisungen, falls count < 0 ; count Zuweisungen andernfalls.

Ausnahmen

Die Überladung mit einem Template-Parameter namens ExecutionPolicy meldet Fehler wie folgt:

  • Wenn die Ausführung einer als Teil des Algorithmus aufgerufenen Funktion eine Exception wirft und ExecutionPolicy einer der Standard-Policies ist, wird std::terminate aufgerufen. Für jede andere ExecutionPolicy ist das Verhalten implementierungsdefiniert.
  • Wenn der Algorithmus keinen Speicher allozieren kann, wird std::bad_alloc geworfen.

Mögliche Implementierung

template<class InputIt, class Size, class OutputIt>
constexpr //< seit C++20
OutputIt copy_n(InputIt first, Size count, OutputIt result)
{
    if (count > 0)
    {
        *result = *first;
        ++result;
        for (Size i = 1; i != count; ++i, (void)++result)
            *result = *++first;
    }
    return result;
}

Beispiel

#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <string>
#include <vector>
int main()
{
    std::string in {"1234567890"};
    std::string out;
    std::copy_n(in.begin(), 4, std::back_inserter(out));
    std::cout << out << '\n';
    std::vector<int> v_in(128);
    std::iota(v_in.begin(), v_in.end(), 1);
    std::vector<int> v_out(v_in.size());
    std::copy_n(v_in.cbegin(), 100, v_out.begin());
    std::cout << std::accumulate(v_out.begin(), v_out.end(), 0) << '\n';
}

Ausgabe:

1234
5050

Siehe auch

kopiert eine Reihe von Elementen an einen neuen Speicherort
(Funktions-Template)
kopiert eine Anzahl von Elementen an einen neuen Speicherort
(Algorithmus-Funktionsobjekt)