简体   繁体   English

C++ 可以使用移动语义将数据从一个向量移动到另一个向量

[英]C++ possible to use move sematics to move data from one vector to another

The code below copies data from one vector into another.下面的代码将数据从一个向量复制到另一个向量。 If the vectors are large then I guess this is expensive.如果向量很大,那么我想这很昂贵。 Is it possible to use move semantics here to copy data from one vector into another?是否可以在这里使用移动语义将数据从一个向量复制到另一个向量?

std::vector<double> newData(Shape.Size(), 0);
std::vector<double> oldData = a->getData();
std::copy(oldData.begin(), oldData.end(), newData.begin());

You could use the algorithm std::move instead of std::copy :您可以使用算法std::move而不是std::copy

std::move(oldData.begin(), oldData.end(), newData.begin());

It will still transfer elements by elements, but will move them instead.它仍然会逐个元素传输元素,但会移动它们。 Don't forget to pre-allocate the vector with the right size to avoid moving elements around too much.不要忘记预先分配具有正确大小的向量,以避免过多地移动元素。

Yes you can do it with std::move_iterator .是的,你可以用std::move_iterator做到这std::move_iterator

std::copy(std::make_move_iterator(oldData.begin()), 
          std::make_move_iterator(oldData.end()), 
          newData.begin());

As @tkausl said this doesn't make much sense for vector of built-in types;正如@tkausl所说,这对于内置类型的vector没有多大意义; copy is performed in this case at last.在这种情况下,最后执行复制。 You might want to move the vector directly, eg您可能想直接移动vector ,例如

std::vector<double> newData = std::move(oldData); // move construction

or或者

newData = std::move(oldData); // move assignment

In the provided example move or copy there is no difference, since values in vector do not perform any spatial actions during move (it a double).在提供的示例移动或复制中没有区别,因为向量中的值在移动期间不执行任何空间操作(它是双精度值)。

Here is reasonable example:这是合理的例子:

 using FooPtr = std::shared_ptr<Foo>;

 constexpr size_t n = 100;
 std::vector<FooPtr> data;
 data.reserve(n);
 std::generate_n(std::back_inserter(data), n, FooFactory);

 std::vector<FooPtr> dst;
 dst.reserve(n);
 std::move(std::begin(data), std::end(data), std::back_inserter(dst));

Note that注意

 dst = std::move(data);

Will be faster since data buffer simply will be reassigned to the other vector.会更快,因为数据缓冲区将简单地重新分配给另一个向量。

https://wandbox.org/permlink/pJnmrWYe40hLRYNE https://wandbox.org/permlink/pJnmrWYe40hLRYNE

Other answer uses make_move_iterator .其他答案使用make_move_iterator This is practical if used with other algorithms.如果与其他算法一起使用,这很实用。 With copy/move is not very practical.用复制/移动不是很实用。

I assuming you are confused about move semantics.我假设您对移动语义感到困惑。 If you must keep your old data belong to old place, there are no way to use move semantics.如果您必须保留属于旧位置的旧数据,则无法使用移动语义。 Move semantic means move data out from old to new.移动语义意味着将数据从旧移到新。

Additional GMT 18:50额外的格林威治标准时间 18:50

#include <iostream>
#include <vector>

#include <cstdlib>
#include <ctime>

#include <cstdint>

int main()
{
    std::srand(std::time(nullptr));

    const int LEN_SHAPE = 1024;
    std::vector<double> a;
    int n = LEN_SHAPE;
    while (n--)
    {
        a.push_back( ((double)std::rand() / RAND_MAX) * std::numeric_limits<double>::max() );
    }

    std::vector<double> newData(a.size(), 0);
    std::vector<double> oldData( std::move( a ));
    std::copy(oldData.begin(), oldData.end(), newData.begin());

}

std::vector oldData( std::move( a )); std::vector oldData( std::move( a )); is a move semantic, check after this what remains in vector a .是移动语义,在此之后检查向量a 中剩余的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM