【发布时间】:2019-08-03 03:06:32
【问题描述】:
自 C++17 起,std::set 具有支持从容器中移动节点的extract() 成员函数。这是一个示例代码:
#include <iostream>
#include <cassert>
#include <set>
struct trace {
trace() {
std::cout << this << ":" << " construct" << std::endl;
}
~trace() {
std::cout << this << ":" << " destruct" << std::endl;
}
trace(trace& other) {
std::cout << this << ":" << " copy construct from " << &other << std::endl;
}
trace(trace&& other) {
std::cout << this << ":" << " move construct from " << &other << std::endl;
}
trace& operator=(trace const& other) {
std::cout << this << ":" << " copy assign from " << &other << std::endl;
return *this;
}
trace& operator=(trace&& other) {
std::cout << this << ":" << " move assign from " << &other << std::endl;
return *this;
}
};
inline bool operator<(trace const& lhs, trace const& rhs) {
std::cout << &lhs << " < " << &rhs << std::endl;
return &lhs < &rhs;
}
int main () {
std::set<trace> s;
s.insert(trace());
s.insert(trace());
s.insert(trace());
auto it = s.begin();
++it;
assert(s.size() == 3);
std::cout << "[[extract]]" << std::endl;
trace t = std::move(s.extract(it).value());
assert(s.size() == 2);
}
运行演示:https://wandbox.org/permlink/ZZHkZV1DUZpM3YrU
我得到了以下结果:
0x7ffd30bbfbd0: construct
0x55edd361d2a0: move construct from 0x7ffd30bbfbd0
0x7ffd30bbfbd0: destruct
0x7ffd30bbfbc8: construct
0x7ffd30bbfbc8 < 0x55edd361d2a0
0x55edd361d2a0 < 0x7ffd30bbfbc8
0x7ffd30bbfbc8 < 0x55edd361d2a0
0x55edd361d2d0: move construct from 0x7ffd30bbfbc8
0x7ffd30bbfbc8: destruct
0x7ffd30bbfbc0: construct
0x7ffd30bbfbc0 < 0x55edd361d2a0
0x7ffd30bbfbc0 < 0x55edd361d2d0
0x55edd361d2d0 < 0x7ffd30bbfbc0
0x7ffd30bbfbc0 < 0x55edd361d2d0
0x55edd361d300: move construct from 0x7ffd30bbfbc0
0x7ffd30bbfbc0: destruct
[[extract]]
0x7ffd30bbfbb0: move construct from 0x55edd361d2d0
0x55edd361d2d0: destruct
0x7ffd30bbfbb0: destruct
0x55edd361d300: destruct
0x55edd361d2a0: destruct
在[[extract]] 之后,容器无法访问从对象移动。没关系。
我正在寻找在 boost::multi_index 上做同样事情的方法。
我尝试了与以下问答相同的方法: Move element from boost multi_index array
我尝试了同样的方法:
namespace mi = boost::multi_index;
using mi_trace = mi::multi_index_container<
trace,
mi::indexed_by<
mi::ordered_unique<
mi::identity<trace>
>
>
>;
int main () {
mi_trace mi;
mi.insert(trace());
mi.insert(trace());
mi.insert(trace());
auto it = mi.begin();
++it;
assert(mi.size() == 3);
std::optional<trace> target;
std::cout << "[[modify]]" << std::endl;
if (mi.modify(
it,
[&](auto& e) {
target.emplace(std::move(e));
}
)
) {
std::cout << "[[erase]]" << std::endl;
mi.erase(it);
assert(mi.size() == 2);
}
}
运行演示:https://wandbox.org/permlink/eKpGDpMBbx5aRz9O
得到以下输出:
[[modify]]
0x7fffe1f77c66: move construct from 0x55fdda3272e0
0x55fdda3272b0 < 0x55fdda3272e0
0x55fdda3272e0 < 0x55fdda327310
[[erase]]
0x55fdda3272e0: destruct
0x7fffe1f77c66: destruct
0x55fdda3272b0: destruct
0x55fdda327310: destruct
multi_index 容器访问在修改 lambda 返回后从对象 (0x55fdda3272e0) 移动。我认为这是为了重新排序。 modify() 不知道我使用 modify() 和 erase()。对于random_access_index,它运行良好,因为容器不需要重新排序,但它不适用于ordered_index。
有没有办法从ordered_indexed multi_index 移动元素?
【问题讨论】:
-
很难理解你的问题。让我们来看第一部分 - 输出对我来说看起来不错。你期望什么输出?
-
第一部分输出没问题。最后一个输出不好,因为移动后访问了0x55fdda3272e0。
-
我想使用
boost::muti_index有序索引做与std::set::extract相同的事情。 -
我仍然不确定您要达到什么目标。你有迭代器
it,它指向你需要的元素,如果你要从中移动什么——只需从容器中移动并删除它。这有什么问题? -
是的,确实有道理。
标签: c++ boost extract multi-index