开发者

Using std::move

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(开发者_如何学JAVA/* 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.


Objects.push_back( std::move( v ) );


vector<vector<Object> > Objects;

for(/* some range */)
{
    vector<Object> v;
    for(/* some other range */)
    {
        v.push_back(/* some object */);
    }
    Objects.push_back(std::move(v));
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜