c++ - boost::bimap 中的移位值

标签 c++ boost c++14 boost-bimap

我有一个像这样的无序 bimap:

using SymPressMap =
    boost::bimap<boost::bimaps::unordered_set_of<sym>,
                 boost::bimaps::unordered_set_of<Press>>;

这基本上是“sym”和“Press”之间的双射。我想循环“Presses”的子集,如图所示:bimap state before and after

这是使用 std::unordered_map 编译但使用 bimap 失败的算法:
void Layout::cycle(SymVector syms) {
  assert(syms.size() >= 2);
  for (auto it = syms.rbegin(); it != syms.rend() - 1; it++) {
    std::swap(sympressmap.left.at(*it), sympressmap.left.at(*(it + 1)));
  }
}

基本思想是连续交换相邻的(就“syms”而言)元素。但我收到了这个错误:
Error   C2678   binary '=': no operator found which takes a left-hand operand of type '_Ty' (or there is no acceptable conversion)  
KeyboardOptimizer   c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.16.27023\include\utility  68  

所以,问题是如何交换bimap中的两个元素? .

UPD:删除插入版本感谢 John Zwinck,它编译
void Layout::cycle(SymVector syms) {
  assert(syms.size() >= 2);
  Press plast = pressmap.left.at(*syms.rbegin());
  pressmap.left.erase(*syms.rbegin());
  for (auto it = syms.rbegin() + 1; it != syms.rend(); it++) {
    auto p = pressmap.left.at(*it);
    pressmap.left.erase(*it);
    pressmap.left.insert(SymPressMap::left_value_type(*(it - 1), p));
  }
  pressmap.left.insert(SymPressMap::left_value_type(*syms.begin(), plast));
}

最佳答案

使用常规 unordered_map,交换 mapped_type values 没有问题,因为容器结构不依赖于它们。但是修改key_type键是一个常见的困难和困惑领域,因为键定义了容器的结构(哪些值在哪个桶中)。

您在这里遇到同样的问题,即您正在尝试修改存储在容器中的键(您是在交换值方面进行的,但在双映射中,键和值当然是对偶的)。你不能那样做。您可以做的是复制键值对,交换它们的值,从容器中删除原件,然后插入修改后的对。

引用:How to change the key in an unordered_map?

关于c++ - boost::bimap 中的移位值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60130649/

相关文章:

c++ - 复合文字是标准 C++ 吗?

c++ - 安置新的返回值

c++ - 这段检查平衡括号的代码是如何工作的?

python - 奇怪的进程因 boost.python 而失败

c++ - 使用 websocketpp 时出现 "No matching function for call to bind"

c++ - 构造不可复制对象的 "array"

c++ - 将 PARENT 类成员的名称作为模板参数传递

c++ - C++ 中的 'and' 关键字

c++ - MFC C映射。将 cmap 存储在 cmap 中

c++ - bool 值的左移和右移