我想通过 Treap 重载运算符像 std::map
那样在 mp[x] = y;
时修改 mp[x]
的值,我是这么写的:
define Pairing(x) Find_by_rk(Rk_of_node(x)).second
V& operator[](K x) { return Pairing(x); }
函数都是字面意思,但是编译器报错:
[Error] cannot bind non-const lvalue reference of type 'int&' to an rvalue of type 'int'
我通过查资料得知:
千万不要返回局部对象的引用。当函数执行完毕时,将释放分配给局部对象的存储空间。此时,对局部对象的引用就会指向不确定的内存
于是我照猫画虎地加了 const
const V& operator[](const K &x) { return Pairing(x); }
但是问题依然没有解决,当我
printf("%d\n", mp[x]);
的时候编译器就卡住了,因为我并没有更改它的值,请问我应该怎么办?