是否可以从std::集中移出一个项目
Is it possible to move an item out of a std::set?
如果我有一个只允许只移动语义的对象,那么可以从集合中移动项吗?我似乎找不到做这件事的方法。
C++17添加了一个函数std::set<>::extract
,该函数允许将对象从集合中移出:
std::set<MoveOnlyType> s;
s.emplace(arg0, arg1, arg2); // only way to insert such move-only objects, since C++11
auto internal_node = s.extract(s.begin()); // internal_node no longer part of set, we can do with it what we want
MoveOnlyType m = std::move(internal_node.value()); // finally get the actual object out
不,这是不可能的。无法获得对set
中元素的非常量访问,并且move需要非常量引用。允许非常量访问将使打破set
的不变量变得非常容易。
相关文章:
- 运行同一解决方案的另一个项目的项目
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 从链接列表c++中删除一个项目
- 在Qt/C++中从一个代码构建两个略有不同的项目
- 如何在 c++ 项目中包含一个包含所有头文件?
- MacOS 上的 Xcode 11 项目不在一个函数中使用 sin 和 cos:未定义的符号"___sincosf_stret"
- 在矢量中使用擦除时双重释放或损坏(快速顶部).如何擦除一个矢量的几个项目知道它们的索引?
- 在macOS上使用libcurl库编译一个c++at项目
- 如何有效地将(一些)项目从一个std::map移动到另一个std::map
- 仅从无序集合中删除一个项目
- 如何在一个简单的C++项目中使用Poco库
- 是否可以将 EXE 文件作为 lib 文件链接到另一个项目?
- 从另一个动态链接库项目调用静态库函数
- 如何在QListWidget中显示QStringList的每个项目QLabel和另一个QString?
- 如何编译和运行一个新的 C++ Actor 框架项目?
- 欧拉项目#8:C++错误的答案(为一个小的炮制输入给出正确的答案)
- 如何使用另一个项目命名空间?
- 如何在不创建新配置的情况下对两个不同解决方案使用的一个项目使用不同的 #defines
- 如何将一个 exe 项目链接到另一个 exe 项目中的类
- NetBeans - 两个项目 - 一个找不到要包含的文件 (C++)