std::copy
Актуально для C++23.
#include <algorithm>
Актуально на 2024-03-07.
Define overload #1
template<class InputIter, class OutputIter> constexpr OutputIter copy(InputIter first, InputIter last, OutputIter out);
Копирует элементы из последовательности [first, last] в "out".
В большинстве реализаций, если first является std::move_iterator, элементы будут перемещены, а не скопированы.
Вернёт OutputIter итератор за последним скопированным элементом или out.
Example, possible implementation
Define overload #2
template<class ExecutionPolicy, class ForwardIter1, class ForwardIter2> ForwardIt2 copy(ExecutionPolicy&& policy, ForwardIter1 first, ForwardIter1 last, ForwardIter2 out);
TODO
Example, possible implementation
Examples
Example 1:
#include <iostream> #include <algorithm> #include <string> #include <vector> int main() { auto data = {0, 1, 3, 5, 7}; std::vector<int> v(5); // скопирует элементы из data в v. std::copy(std::begin(data), std::end(data), v.begin()); for (auto x : v) { std::cout << x << std::endl; } return 0; }
0 1 3 5 7
Example 2:
#include <iostream> #include <algorithm> #include <string> #include <vector> #include <iterator> int main() { std::vector<std::string> out(2); std::vector<std::string> in{{"zxcvbnm"}, {"asdfghjkl"}}; // переместит std::string из одного std::vector в другой. std::copy(std::make_move_iterator(std::begin(in)), std::make_move_iterator(std::end(in)), out.begin()); for (auto x : in) { std::cout << "in: " << x << std::endl; } for (auto x : out) { std::cout << "out: " << x << std::endl; } return 0; }
in: in: out: zxcvbnm out: asdfghjkl
Changelog
C++26
TODOC++23
TODOC++20
TODOC++17
TODOC++14
TODOC++11
TODOSee also
TODO
This page was last modified on 2024-03-07