C ++ 0x“从”容器 [英] C++0x "move from" container

查看:101
本文介绍了C ++ 0x“从”容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 0x中,我们通过 std :: move 获得了有关容器的效率提升:

In C++0x, we get an efficiency boost concerning containers with std::move:

SomeExpensiveType x = /* ... */;
vec.push_back(std::move(x));

但我找不到任何其他方式。我的意思是这样:

But I can't find anything going the other way. What I mean is something like this:

SomeExpensiveType x = vec.back(); // copy!
vec.pop_back(); // argh

这在适配器的 stack 。可能有这样的存在:

This is more frequent (the copy-pop) on adapter's like stack. Could something like this exist:

SomeExpensiveType x = vec.move_back(); // move and pop

避免复制?这是否已经存在?我在n3000找不到类似的东西。

To avoid a copy? And does this already exist? I couldn't find anything like that in n3000.

我有一种感觉,我错过了一些显而易见的东西(像它的不必要的东西),所以我准备ru dum。 :3

I have a feeling I'm missing something painfully obvious (like the needlessness of it), so I am prepared for "ru dum". :3

推荐答案

我可能完全错了,但不是你想要的

I might be total wrong here, but isn't what you want just

SomeExpensiveType x = std::move( vec.back() ); vec.pop_back();

假设SomeExpensiveType有一个移动构造函数。 (显然对你的情况)

Assuming SomeExpensiveType has a move constructor. (and obviously true for your case)

这篇关于C ++ 0x“从”容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆