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
?
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));
}
精彩评论