Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using std::move

Tags:

c++

c++11

Before C++11 I have used swap-to-back to avoid deep copy overheads, like:

vector<vector<Object> > Objects;

for(/* some range */)
{
    vector<Object> v;
    for(/* some other range */)
    {
        v.push_back(/* some object */);
    }
    Objects.push_back(vector<Object>());
    Objects.back().swap(v);
}

How can I use std::move to move v into Objects to avoid deep copy overhead instead of swap?
I know there are a lot of workarounds here like multi arrays or just inserting directly into Objects.back(), but I need an example of usage of std::move to understand it.

like image 349
Dani Avatar asked Oct 11 '11 23:10

Dani


1 Answers

Objects.push_back( std::move( v ) );
like image 184
K-ballo Avatar answered Oct 21 '22 03:10

K-ballo