@specsoftdev live:.cid.8e17e9b93cabb607 specsoftdev@gmail.com
std::move
Актуально для C++23.

#include <algorithm>
Актуально на 2024-03-02.



Define overload #1
template<class InputIter, class OutputIter>
constexpr OutputIter
copy(InputIter first, InputIter last, OutputIter out);

Копирует элементы из последовательности [first, last] в "out".
В большинстве реализаций, если first является std::move_iterator, элементы будут перемещены, а не скопированы.
Вернёт OutputIterator итератор 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
TODO
C++23
TODO
C++20
TODO
C++17
TODO
C++14
TODO
C++11
TODO


See also

TODO

This page was last modified on 2024-03-02