std::copy, std::copy_if – cppreference.com
Aus cppreference.com
<metanoindex/>
<tbody> </tbody>
| definiert in Header <algorithm> |
||
|
|
(1) | |
|
|
(2) | (seit C++11) |
Kopiert die Elemente im Bereich von [first, last) definiert, in einen anderen Bereich beginnend bei d_first. Die zweite Funktion kopiert nur die Elemente, für die das Prädikat pred kehrt true .
Original:
Copies the elements in the range, defined by [first, last), to another range beginning at d_first. The second function only copies the elements for which the predicate pred returns true.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Parameter
| first, last | - | der Bereich der zu kopierenden Elemente Original: the range of elements to copy The text has been machine-translated via Google Translate. |
| d_first | - | der Beginn des Zielbereichs. Wenn Original: the beginning of the destination range. If The text has been machine-translated via Google Translate. |
| pred | - | unary predicate which returns true für die erforderlichen Elemente Original: for the required elements The text has been machine-translated via Google Translate. The signature of the predicate function should be equivalent to the following:
The signature does not need to have |
| Type requirements | ||
-InputIt must meet the requirements of InputIterator.
| ||
-OutputIt must meet the requirements of OutputIterator.
| ||
Rückgabewert
Output Iterator auf das Element in den Zielbereich kopiert, ein nach dem letzten Element .
Original:
Output iterator to the element in the destination range, one past the last element copied.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Komplexität
1)
Genau last - first Aufgaben
Original:
Exactly last - first assignments
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
Genau last - first Anwendungen des Prädikats
Original:
Exactly last - first applications of the predicate
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Notes
In der Praxis vermeiden Implementierungen std::copy mehrere Zuweisungen und Nutzung Bulk-Copy-Funktionen wie std::memcpy wenn der Werttyp ist TriviallyCopyable
Original:
In practice, implementations of std::copy avoid multiple assignments and use bulk copy functions such as std::memcpy if the value type is TriviallyCopyable
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Mögliche Implementierung
| First version |
|---|
template<class InputIt, class OutputIt> OutputIt copy(InputIt first, InputIt last, OutputIt d_first) { while (first != last) { *d_first++ = *first++; } return d_first; } |
| Second version |
template<class InputIt, class OutputIt, class UnaryPredicate> OutputIt copy_if(InputIt first, InputIt last, OutputIt d_first, UnaryPredicate pred) { while (first != last) { if(pred(*first)) *d_first++ = *first; first++; } return d_first; } |
Beispiel
Der folgende Code verwendet copy sowohl kopieren Sie den Inhalt eines Vektors zu einem anderen und um das resultierende Vektor:
Original:
The following code uses copy to both copy the contents of one vector to another and to display the resulting vector:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
#include <algorithm> #include <iostream> #include <vector> #include <iterator> int main() { std::vector<int> from_vector; for (int i = 0; i < 10; i++) { from_vector.push_back(i); } std::vector<int> to_vector(10); std::copy(from_vector.begin(), from_vector.end(), to_vector.begin()); std::cout << "to_vector contains: "; std::copy(to_vector.begin(), to_vector.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << std::endl; }
Output:
to_vector contains: 0 1 2 3 4 5 6 7 8 9
Siehe auch
Kopien einen Bereich von Elementen in rückwärts um Original: copies a range of elements in backwards order The text has been machine-translated via Google Translate. (Funktions-Template) [edit] | |
Kopiert einen Bereich von Elementen ohne die Seiten, die bestimmte Kriterien erfüllen Original: copies a range of elements omitting those that satisfy specific criteria The text has been machine-translated via Google Translate. (Funktions-Template) [edit] | |