提升:使用运算符 [] 访问 bimap

Boost: Accessing bimap using operator[]

本文关键字:访问 bimap 运算符 提升      更新时间:2023-10-16

在下面的代码中:

typedef bimap< set_of< std::string >, list_of< int > > bm_type;
bm_type bm;
bm.left["one"] = 1; // "one" -> 1
bm.left["one"] = 2; // replaced: "one" -> 2
bm.right[2] = "two"; // Compile error

如何摆脱编译错误?难道不能用operator[]访问正确的视图吗?

list_of没有operator[]

看看 http://www.boost.org/doc/libs/1_47_0/libs/bimap/doc/html/boost_bimap/reference/list_of_reference.html

这段代码没有任何意义。列表没有operator[]因为您必须选择在创建元素时插入元素的位置。由于列表不是内部排序的(如地图),因此2可以位于列表的开头、列表的末尾、列表的中间或其他任何位置。

(Boost的list_of模仿标准list的语义。