关于 stl map 的 []操作符问题

来源:互联网 发布:js 构造函数 编辑:程序博客网 时间:2024/06/11 03:18

stl 文档中是如下描述:

Access element
If k matches the key of an element in the container, the function returns a reference to its mapped value.

If k does not match the key of any element in the container, the function inserts a new element with that key and returns a reference to its mapped value. Notice that this always increases the container size by one, even if no mapped value is assigned to the element (the element is constructed using its default constructor).
注意斜体字部分,也就是说,如果你的对象想保存在 map 中并且要用到[] 那么你的类起码要实现以下几个函数:无参数的构造函数   --- 对应元素不存在时 map 自行创建。operator =          ----- 用于  通过 [] 操作符来赋值拷贝构造函数         ---- 可能要用到,没具体研究,反正实现了总比不实现要好。